~neon/project-neon/kscreen

« back to all changes in this revision

Viewing changes to kcm/src/modeselectionwidget.cpp

  • Committer: Dan Vrátil
  • Date: 2012-11-13 11:22:10 UTC
  • mfrom: (0.1.48)
  • Revision ID: git-v1:14fdbb224dc8a85b623b394662458a62675e977c
Merge branch 'kcm_import'

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    <one line to give the program's name and a brief idea of what it does.>
 
3
    Copyright (C) 2012  Dan Vratil <dan@progdan.cz>
 
4
 
 
5
    This program is free software: you can redistribute it and/or modify
 
6
    it under the terms of the GNU General Public License as published by
 
7
    the Free Software Foundation, either version 3 of the License, or
 
8
    (at your option) any later version.
 
9
 
 
10
    This program is distributed in the hope that it will be useful,
 
11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
    GNU General Public License for more details.
 
14
 
 
15
    You should have received a copy of the GNU General Public License
 
16
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
17
*/
 
18
 
 
19
 
 
20
#include "modeselectionwidget.h"
 
21
 
 
22
#include <QListView>
 
23
#include <QGridLayout>
 
24
#include <kscreen/output.h>
 
25
#include <KDebug>
 
26
 
 
27
#include "qmloutput.h"
 
28
#include "modesproxymodel.h"
 
29
#include "resolutionsortmodel.h"
 
30
 
 
31
ModeSelectionWidget::ModeSelectionWidget(QGraphicsItem *parent, Qt::WindowFlags wFlags):
 
32
    QGraphicsProxyWidget(parent, wFlags),
 
33
    m_output(0)
 
34
{
 
35
    m_resolutionsModel = new ResolutionSortModel(this);
 
36
 
 
37
    m_resolutionsView = new QListView();
 
38
    m_resolutionsView->setModel(m_resolutionsModel);
 
39
    connect(m_resolutionsView, SIGNAL(clicked(QModelIndex)), SLOT(resolutionChanged(QModelIndex)));
 
40
 
 
41
    m_refreshRatesModel = new ModesProxyModel(this);
 
42
 
 
43
    m_refreshRatesView = new QListView();
 
44
    m_refreshRatesView->setModel(m_refreshRatesModel);
 
45
    connect(m_resolutionsView, SIGNAL(clicked(QModelIndex)), SLOT(refreshRateChanged()));
 
46
 
 
47
    QWidget *rootWidget = new QWidget();
 
48
 
 
49
    QGridLayout *mainLayout = new QGridLayout(rootWidget);
 
50
    mainLayout->addWidget(m_resolutionsView, 0, 0);
 
51
    mainLayout->addWidget(m_refreshRatesView, 0, 1);
 
52
    rootWidget->setLayout(mainLayout);
 
53
 
 
54
    setWidget(rootWidget);
 
55
}
 
56
 
 
57
ModeSelectionWidget::~ModeSelectionWidget()
 
58
{
 
59
 
 
60
}
 
61
 
 
62
 
 
63
void ModeSelectionWidget::setOutput(QMLOutput *output)
 
64
{
 
65
    m_output = output;
 
66
 
 
67
    if (!output) {
 
68
          return;
 
69
    }
 
70
 
 
71
    m_resolutionsModel->setSourceModel(output->modesModel());
 
72
    m_resolutionsModel->sort(0, Qt::DescendingOrder);
 
73
 
 
74
    m_refreshRatesModel->setSourceModel(m_resolutionsModel);
 
75
 
 
76
    KScreen::Mode *currentMode = m_output->output()->mode(m_output->output()->currentMode());
 
77
    if (!currentMode) {
 
78
        return;
 
79
    }
 
80
 
 
81
    for (int i = 0; i < m_resolutionsModel->rowCount(); i++) {
 
82
          QSize size = m_resolutionsModel->index(i, 0).data(QMLOutput::SizeRole).toSize();
 
83
 
 
84
          if (size == currentMode->size()) {
 
85
            QModelIndex index = m_resolutionsModel->index(i, 0);
 
86
            m_resolutionsView->setCurrentIndex(index);
 
87
            resolutionChanged(index);
 
88
            break;
 
89
          }
 
90
    }
 
91
}
 
92
 
 
93
QMLOutput *ModeSelectionWidget::output() const
 
94
{
 
95
    return m_output;
 
96
}
 
97
 
 
98
void ModeSelectionWidget::resolutionChanged(const QModelIndex &index)
 
99
{
 
100
    m_refreshRatesModel->setSourceModelCurrentRow(index.row());
 
101
 
 
102
    if (!m_refreshRatesView->currentIndex().isValid()) {
 
103
        m_refreshRatesView->setCurrentIndex(m_refreshRatesModel->index(0, 0));
 
104
    } else {
 
105
        refreshRateChanged();
 
106
    }
 
107
}
 
108
 
 
109
void ModeSelectionWidget::refreshRateChanged()
 
110
{
 
111
    if (!m_output) {
 
112
        return;
 
113
    }
 
114
 
 
115
    QModelIndex proxyModelIndex = m_resolutionsModel->index(m_resolutionsView->currentIndex().row(), 0);
 
116
    QModelIndex parentIndex = m_resolutionsModel->mapToSource(proxyModelIndex);
 
117
    QModelIndex modelIndex = m_refreshRatesView->model()->index(m_refreshRatesView->currentIndex().row(), 0, parentIndex);
 
118
 
 
119
    int modeId = m_refreshRatesView->model()->data(modelIndex, QMLOutput::ModeIdRole).toInt();
 
120
    if (modeId == -1) {
 
121
        QModelIndex proxyModelIndex = m_resolutionsModel->index(m_resolutionsView->currentIndex().row(), 0);
 
122
        QModelIndex parentIndex = m_resolutionsModel->mapToSource(proxyModelIndex);
 
123
        modeId = m_output->modesModel()->index(0, 0, parentIndex).data(QMLOutput::ModeIdRole).toInt();
 
124
    }
 
125
 
 
126
    if (modeId == 0) {
 
127
        return;
 
128
    }
 
129
 
 
130
    m_output->output()->setCurrentMode(modeId);
 
131
    m_refreshRatesView->repaint();
 
132
}
 
133
 
 
134
#include "modeselectionwidget.moc"
 
 
b'\\ No newline at end of file'