~ubuntu-branches/ubuntu/wily/kscreen/wily

« back to all changes in this revision

Viewing changes to kcm/src/modeselectionwidget.cpp

  • Committer: Package Import Robot
  • Author(s): Scarlett Clark, Scarlett Clark, Jonathan Riddell, Harald Sitter
  • Date: 2014-08-20 08:35:15 UTC
  • mfrom: (1.1.7)
  • Revision ID: package-import@ubuntu.com-20140820083515-i9lk9nyt0adwd2q5
Tags: 2.0.0~git20141114-0ubuntu1
[ Scarlett Clark ]
* Update packaging for frameworks branch
* Git snapshot of the frameworks branch

[ Jonathan Riddell ]
* Remove kscreen-console.1 manpage which is out of date

[ Harald Sitter ]
* switch to new pkg-kde-tools

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
    Copyright (C) 2012  Dan Vratil <dvratil@redhat.com>
3
 
 
4
 
    This library is free software; you can redistribute it and/or
5
 
    modify it under the terms of the GNU Lesser General Public
6
 
    License as published by the Free Software Foundation; either
7
 
    version 2.1 of the License, or (at your option) any later version.
8
 
 
9
 
    This library is distributed in the hope that it will be useful,
10
 
    but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
 
    Lesser General Public License for more details.
13
 
 
14
 
    You should have received a copy of the GNU Lesser General Public
15
 
    License along with this library; if not, write to the Free Software
16
 
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17
 
*/
18
 
 
19
 
#include "modeselectionwidget.h"
20
 
 
21
 
#include <QListView>
22
 
#include <QGridLayout>
23
 
#include <QPainter>
24
 
#include <QGraphicsProxyWidget>
25
 
#include <kscreen/output.h>
26
 
#include <KDebug>
27
 
#include <QRectF>
28
 
#include <sys/socket.h>
29
 
 
30
 
#include "qmloutput.h"
31
 
#include "modesproxymodel.h"
32
 
#include "resolutionsortmodel.h"
33
 
 
34
 
ModeSelectionWidget::ModeSelectionWidget(QDeclarativeItem *parent):
35
 
    QDeclarativeItem(parent),
36
 
    m_output(0),
37
 
    m_refreshRatesModel(new ModesProxyModel(this)),
38
 
    m_resolutionsModel(new ResolutionSortModel(this))
39
 
{
40
 
    setFlag(QGraphicsItem::ItemHasNoContents, false);
41
 
 
42
 
    rootWidget = new QWidget();
43
 
 
44
 
    m_resolutionsView = new QListView(rootWidget);
45
 
    m_resolutionsView->setModel(m_resolutionsModel);
46
 
    m_resolutionsView->setEditTriggers(QAbstractItemView::NoEditTriggers);
47
 
    connect(m_resolutionsView, SIGNAL(clicked(QModelIndex)), SLOT(resolutionChanged(QModelIndex)));
48
 
    connect(m_resolutionsView, SIGNAL(doubleClicked(QModelIndex)), SLOT(acceptMode(QModelIndex)));
49
 
 
50
 
    m_refreshRatesView = new QListView(rootWidget);
51
 
    m_refreshRatesView->setModel(m_refreshRatesModel);
52
 
    m_refreshRatesView->setEditTriggers(QAbstractItemView::NoEditTriggers);
53
 
    connect(m_refreshRatesView, SIGNAL(clicked(QModelIndex)), SLOT(refreshRateChanged()));
54
 
    connect(m_refreshRatesView, SIGNAL(doubleClicked(QModelIndex)), SLOT(acceptMode(QModelIndex)));
55
 
 
56
 
    QGridLayout *mainLayout = new QGridLayout(rootWidget);
57
 
    mainLayout->addWidget(m_resolutionsView, 0, 0);
58
 
    mainLayout->addWidget(m_refreshRatesView, 0, 1);
59
 
    rootWidget->setLayout(mainLayout);
60
 
    rootWidget->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
61
 
    rootWidget->setMaximumSize(300, 250);
62
 
    rootWidget->setAttribute(Qt::WA_TranslucentBackground);
63
 
 
64
 
    m_proxyWidget = new QGraphicsProxyWidget(this);
65
 
    m_proxyWidget->setWidget(rootWidget);
66
 
 
67
 
    /* FIXME: Shouldn't this be automatic? */
68
 
    setWidth(m_proxyWidget->geometry().width());
69
 
    setHeight(m_proxyWidget->geometry().height());
70
 
}
71
 
 
72
 
ModeSelectionWidget::~ModeSelectionWidget()
73
 
{
74
 
 
75
 
}
76
 
 
77
 
void ModeSelectionWidget::setOutput(QMLOutput *output)
78
 
{
79
 
    m_output = output;
80
 
 
81
 
    if (!output) {
82
 
        return;
83
 
    }
84
 
 
85
 
    m_resolutionsModel->setSourceModel(output->modesModel());
86
 
    m_resolutionsModel->sort(0, Qt::DescendingOrder);
87
 
 
88
 
    m_refreshRatesModel->setSourceModel(m_resolutionsModel);
89
 
 
90
 
    KScreen::Mode *currentMode = m_output->output()->currentMode();
91
 
    if (!currentMode) {
92
 
        return;
93
 
    }
94
 
 
95
 
    for (int i = 0; i < m_resolutionsModel->rowCount(); i++) {
96
 
        QSize size = m_resolutionsModel->index(i, 0).data(QMLOutput::SizeRole).toSize();
97
 
 
98
 
        if (size == currentMode->size()) {
99
 
            QModelIndex index = m_resolutionsModel->index(i, 0);
100
 
            m_resolutionsView->setCurrentIndex(index);
101
 
            resolutionChanged(index);
102
 
            break;
103
 
        }
104
 
    }
105
 
}
106
 
 
107
 
QMLOutput *ModeSelectionWidget::output() const
108
 
{
109
 
    return m_output;
110
 
}
111
 
 
112
 
void ModeSelectionWidget::resolutionChanged(const QModelIndex &index)
113
 
{
114
 
    m_refreshRatesModel->setSourceModelCurrentRow(index.row());
115
 
 
116
 
    if (!m_refreshRatesView->currentIndex().isValid()) {
117
 
        m_refreshRatesView->setCurrentIndex(m_refreshRatesModel->index(0, 0));
118
 
    } else {
119
 
        refreshRateChanged();
120
 
    }
121
 
}
122
 
 
123
 
void ModeSelectionWidget::acceptMode(const QModelIndex &index)
124
 
{
125
 
    if (!index.isValid()) {
126
 
        return;
127
 
    }
128
 
 
129
 
    if (sender() == m_refreshRatesView) {
130
 
        refreshRateChanged();
131
 
        Q_EMIT accepted();
132
 
    } else if (sender() == m_resolutionsView) {
133
 
        resolutionChanged(index);
134
 
        Q_EMIT accepted();
135
 
    }
136
 
}
137
 
 
138
 
 
139
 
void ModeSelectionWidget::refreshRateChanged()
140
 
{
141
 
    if (!m_output) {
142
 
        return;
143
 
    }
144
 
 
145
 
    QModelIndex proxyModelIndex = m_resolutionsModel->index(m_resolutionsView->currentIndex().row(), 0);
146
 
    QModelIndex parentIndex = m_resolutionsModel->mapToSource(proxyModelIndex);
147
 
    QModelIndex modelIndex = m_refreshRatesView->model()->index(m_refreshRatesView->currentIndex().row(), 0, parentIndex);
148
 
 
149
 
    QString modeId = m_refreshRatesView->model()->data(modelIndex, QMLOutput::ModeIdRole).toString();
150
 
    if (modeId == QLatin1String("-1")) {
151
 
        QModelIndex proxyModelIndex = m_resolutionsModel->index(m_resolutionsView->currentIndex().row(), 0);
152
 
        QModelIndex parentIndex = m_resolutionsModel->mapToSource(proxyModelIndex);
153
 
        modeId = m_output->modesModel()->index(0, 0, parentIndex).data(QMLOutput::ModeIdRole).toString();
154
 
    }
155
 
 
156
 
    if (modeId == QLatin1String("0")) {
157
 
        return;
158
 
    }
159
 
 
160
 
    m_output->output()->setCurrentModeId(modeId);
161
 
    m_refreshRatesView->repaint();
162
 
}
163
 
 
164
 
#include "modeselectionwidget.moc"