resolutionslider.cpp 6.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * Copyright 2013  Daniel Vrátil <dvratil@redhat.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License or (at your option) version 3 or any later version
 * accepted by the membership of KDE e.V. (or its successor approved
 * by the membership of KDE e.V.), which shall act as a proxy
 * defined in Section 14 of version 3 of the license.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "resolutionslider.h"
23
#include "utils.h"
24

25 26 27
#include <QGridLayout>
#include <QLabel>
#include <QSlider>
28
#include <QComboBox>
29

30
#include <KLocalizedString>
31

32 33 34 35 36 37 38
#include <kscreen/output.h>

static bool sizeLessThan(const QSize &sizeA, const QSize &sizeB)
{
    return sizeA.width() * sizeA.height() < sizeB.width() * sizeB.height();
}

39
ResolutionSlider::ResolutionSlider(const KScreen::OutputPtr &output, QWidget *parent)
40 41 42
    : QWidget(parent)
    , mOutput(output)
{
43 44
    connect(output.data(), &KScreen::Output::currentModeIdChanged,
            this, &ResolutionSlider::slotOutputModeChanged);
45 46
    connect(output.data(), &KScreen::Output::modesChanged,
            this, &ResolutionSlider::init);
47

48 49 50 51 52 53 54 55 56 57 58
    init();
}

ResolutionSlider::~ResolutionSlider()
{
}

void ResolutionSlider::init()
{
    mModes.clear();
    Q_FOREACH (const KScreen::ModePtr &mode, mOutput->modes()) {
59 60 61 62 63 64 65 66
        if (mModes.contains(mode->size())) {
            continue;
        }

        mModes << mode->size();
    }
    qSort(mModes.begin(), mModes.end(), sizeLessThan);

67 68 69 70 71 72 73 74 75 76 77 78
    delete layout();
    delete mSmallestLabel;
    mSmallestLabel = nullptr;
    delete mBiggestLabel;
    mBiggestLabel = nullptr;
    delete mCurrentLabel;
    mCurrentLabel = nullptr;
    delete mSlider;
    mSlider = nullptr;
    delete mComboBox;
    mComboBox = nullptr;

79
    QGridLayout *layout = new QGridLayout(this);
80 81
    int margin = layout->margin();
    // Avoid double margins
Laurent Montel's avatar
Laurent Montel committed
82
    layout->setContentsMargins(0, 0, 0, 0);
83 84 85 86 87 88

    if (mModes.count() > 15) {
        std::reverse(mModes.begin(), mModes.end());
        mComboBox = new QComboBox(this);
        mComboBox->setSizeAdjustPolicy(QComboBox::AdjustToContents);
        mComboBox->setEditable(false);
89 90
        int currentModeIndex = -1;
        int preferredModeIndex = -1;
91 92
        Q_FOREACH (const QSize &size, mModes) {
            mComboBox->addItem(Utils::sizeToString(size));
93
            if (mOutput->currentMode() && (mOutput->currentMode()->size() == size)) {
94
                currentModeIndex = mComboBox->count() - 1;
95
            } else if (mOutput->preferredMode() && (mOutput->preferredMode()->size() == size)) {
96
                preferredModeIndex = mComboBox->count() - 1;
97 98
            }
        }
99 100 101 102 103
        if (currentModeIndex != -1) {
            mComboBox->setCurrentIndex(currentModeIndex);
        } else if (preferredModeIndex != -1) {
            mComboBox->setCurrentIndex(preferredModeIndex);
        }
104 105
        layout->addWidget(mComboBox, 0, 0, 1, 1);
        connect(mComboBox, static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
106 107 108
                this, &ResolutionSlider::slotValueChanged, Qt::UniqueConnection);

        Q_EMIT resolutionChanged(mModes.at(mComboBox->currentIndex()));
109 110 111 112 113
    } else {
        mCurrentLabel = new QLabel(this);
        mCurrentLabel->setAlignment(Qt::AlignCenter);
        layout->addWidget(mCurrentLabel, 1, 0, 1, 3);

Laurent Montel's avatar
Laurent Montel committed
114
        if (mModes.isEmpty()) {
115
            mCurrentLabel->setText(i18n("No available resolutions"));
116
        } else if (mModes.count() == 1) {
117
            mCurrentLabel->setText(Utils::sizeToString(mModes.first()));
118
        } else {
119 120
            // No double margins left and right, but they are needed on top and bottom
            layout->setContentsMargins(0, margin, 0, margin);
121 122 123 124 125 126 127 128
            mSlider = new QSlider(Qt::Horizontal, this);
            mSlider->setTickInterval(1);
            mSlider->setTickPosition(QSlider::TicksBelow);
            mSlider->setSingleStep(1);
            mSlider->setPageStep(1);
            mSlider->setMinimum(0);
            mSlider->setMaximum(mModes.size() - 1);
            mSlider->setSingleStep(1);
129 130 131 132
            if (mOutput->currentMode()) {
                mSlider->setValue(mModes.indexOf(mOutput->currentMode()->size()));
            } else if (mOutput->preferredMode()) {
                mSlider->setValue(mModes.indexOf(mOutput->preferredMode()->size()));
133 134 135 136 137 138 139 140 141 142 143 144 145 146
            } else {
                mSlider->setValue(mSlider->maximum());
            }
            layout->addWidget(mSlider, 0, 1);
            connect(mSlider, &QSlider::valueChanged,
                    this, &ResolutionSlider::slotValueChanged);

            mSmallestLabel = new QLabel(this);
            mSmallestLabel->setText(Utils::sizeToString(mModes.first()));
            layout->addWidget(mSmallestLabel, 0, 0);
            mBiggestLabel = new QLabel(this);
            mBiggestLabel->setText(Utils::sizeToString(mModes.last()));
            layout->addWidget(mBiggestLabel, 0, 2);

147 148 149
            const auto size = mModes.at(mSlider->value());
            mCurrentLabel->setText(Utils::sizeToString(size));
            Q_EMIT resolutionChanged(size);
150
        }
151
    }
152 153
}

154 155 156 157 158 159 160 161 162 163
QSize ResolutionSlider::currentResolution() const
{
    if (mModes.isEmpty()) {
        return QSize();
    }

    if (mModes.size() < 2) {
        return mModes.first();
    }

164 165 166 167 168 169
    if (mSlider) {
        return mModes.at(mSlider->value());
    } else {
        const int i = mComboBox->currentIndex();
        return i > -1 ? mModes.at(i) : QSize();
    }
170 171
}

172 173 174 175 176 177
void ResolutionSlider::slotOutputModeChanged()
{
    if (!mOutput->currentMode()) {
        return;
    }

178 179 180 181
    if (mSlider) {
        mSlider->blockSignals(true);
        mSlider->setValue(mModes.indexOf(mOutput->currentMode()->size()));
        mSlider->blockSignals(false);
182
    } else if (mComboBox) {
183 184 185 186
        mComboBox->blockSignals(true);
        mComboBox->setCurrentIndex(mModes.indexOf(mOutput->currentMode()->size()));
        mComboBox->blockSignals(false);
    }
187 188
}

189
void ResolutionSlider::slotValueChanged(int value)
190 191
{
    const QSize &size = mModes.at(value);
192 193 194
    if (mCurrentLabel) {
        mCurrentLabel->setText(Utils::sizeToString(size));
    }
195 196 197

    Q_EMIT resolutionChanged(size);
}