2
Copyright (C) 2012 Dan Vratil <dvratil@redhat.com>
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.
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.
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
19
#include "modeselectionwidget.h"
22
#include <QGridLayout>
24
#include <QGraphicsProxyWidget>
25
#include <kscreen/output.h>
28
#include <sys/socket.h>
30
#include "qmloutput.h"
31
#include "modesproxymodel.h"
32
#include "resolutionsortmodel.h"
34
ModeSelectionWidget::ModeSelectionWidget(QDeclarativeItem *parent):
35
QDeclarativeItem(parent),
37
m_refreshRatesModel(new ModesProxyModel(this)),
38
m_resolutionsModel(new ResolutionSortModel(this))
40
setFlag(QGraphicsItem::ItemHasNoContents, false);
42
rootWidget = new QWidget();
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)));
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)));
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);
64
m_proxyWidget = new QGraphicsProxyWidget(this);
65
m_proxyWidget->setWidget(rootWidget);
67
/* FIXME: Shouldn't this be automatic? */
68
setWidth(m_proxyWidget->geometry().width());
69
setHeight(m_proxyWidget->geometry().height());
72
ModeSelectionWidget::~ModeSelectionWidget()
77
void ModeSelectionWidget::setOutput(QMLOutput *output)
85
m_resolutionsModel->setSourceModel(output->modesModel());
86
m_resolutionsModel->sort(0, Qt::DescendingOrder);
88
m_refreshRatesModel->setSourceModel(m_resolutionsModel);
90
KScreen::Mode *currentMode = m_output->output()->currentMode();
95
for (int i = 0; i < m_resolutionsModel->rowCount(); i++) {
96
QSize size = m_resolutionsModel->index(i, 0).data(QMLOutput::SizeRole).toSize();
98
if (size == currentMode->size()) {
99
QModelIndex index = m_resolutionsModel->index(i, 0);
100
m_resolutionsView->setCurrentIndex(index);
101
resolutionChanged(index);
107
QMLOutput *ModeSelectionWidget::output() const
112
void ModeSelectionWidget::resolutionChanged(const QModelIndex &index)
114
m_refreshRatesModel->setSourceModelCurrentRow(index.row());
116
if (!m_refreshRatesView->currentIndex().isValid()) {
117
m_refreshRatesView->setCurrentIndex(m_refreshRatesModel->index(0, 0));
119
refreshRateChanged();
123
void ModeSelectionWidget::acceptMode(const QModelIndex &index)
125
if (!index.isValid()) {
129
if (sender() == m_refreshRatesView) {
130
refreshRateChanged();
132
} else if (sender() == m_resolutionsView) {
133
resolutionChanged(index);
139
void ModeSelectionWidget::refreshRateChanged()
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);
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();
156
if (modeId == QLatin1String("0")) {
160
m_output->output()->setCurrentModeId(modeId);
161
m_refreshRatesView->repaint();
164
#include "modeselectionwidget.moc"