2 * Copyright (c) 2007, 2008 Harry Bock <hbock@providence.edu>
3 * Copyright (c) 2007 Gustavo Pichorim Boiko <gustavo.boiko@kdemail.net>
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 2 of the License, or
8 * (at your option) any later version.
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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #include "outputconfig.h"
22 #include "outputgraphicsitem.h"
23 #include "collapsiblewidget.h"
24 #include "layoutmanager.h"
26 #include "randrconfig.h"
27 #include "randroutput.h"
28 #include "randrdisplay.h"
29 #include "randrscreen.h"
31 #include <kglobalsettings.h>
33 RandRConfig::RandRConfig(QWidget
*parent
, RandRDisplay
*display
)
34 : QWidget(parent
), Ui::RandRConfigBase()
42 if (!m_display
->isValid()) {
43 // FIXME: this needs much better handling of this error...
49 connect( identifyOutputsButton
, SIGNAL( clicked()), SLOT( identifyOutputs()));
50 connect( &identifyTimer
, SIGNAL( timeout()), SLOT( clearIndicators()));
51 identifyTimer
.setSingleShot( true );
53 // create the container for the settings widget
54 QHBoxLayout
*layout
= new QHBoxLayout(outputList
);
55 layout
->setSpacing(0);
56 layout
->setContentsMargins(0,0,0,0);
57 m_container
= new SettingsContainer(outputList
);
58 m_container
->setSizePolicy(QSizePolicy::Minimum
,
59 QSizePolicy::Minimum
);
60 layout
->addWidget(m_container
);
63 m_scene
= new QGraphicsScene(m_display
->currentScreen()->rect());
64 screenView
->setScene(m_scene
);
65 screenView
->installEventFilter(this);
67 m_layoutManager
= new LayoutManager(m_display
->currentScreen(), m_scene
);
70 RandRConfig::~RandRConfig()
75 void RandRConfig::load(void)
77 if (!m_display
->isValid()) {
78 kDebug() << "Invalid display! Aborting config load.";
83 qDeleteAll(m_outputList
);
86 QList
<QGraphicsItem
*> items
= m_scene
->items();
87 foreach(QGraphicsItem
*i
, items
) {
88 if(i
->scene() == m_scene
)
89 m_scene
->removeItem(i
);
95 OutputMap outputs
= m_display
->currentScreen()->outputs();
97 // FIXME: adjust it to run on a multi screen system
99 OutputGraphicsItem
*o
;
100 foreach(RandROutput
*output
, outputs
)
102 o
= new OutputGraphicsItem(output
);
105 connect(o
, SIGNAL(itemChanged(OutputGraphicsItem
*)),
106 this, SLOT(slotAdjustOutput(OutputGraphicsItem
*)));
108 OutputConfig
*config
= new OutputConfig(0, output
, o
);
110 QString description
= output
->isConnected()
111 ? i18n("%1 (Connected)", output
->name())
113 w
= m_container
->insertWidget(config
, description
);
114 if(output
->isConnected()) {
115 w
->setExpanded(true);
116 kDebug() << "Output rect:" << output
->rect();
118 m_outputList
.append(w
);
120 connect(config
, SIGNAL(updateView()), this, SLOT(slotUpdateView()));
121 connect(config
, SIGNAL(optionChanged()), this, SLOT(slotChanged()));
126 void RandRConfig::save()
128 if (!m_display
->isValid())
134 void RandRConfig::defaults()
139 void RandRConfig::apply()
141 kDebug() << "Applying settings...";
142 foreach(CollapsibleWidget
*w
, m_outputList
) {
143 OutputConfig
*config
= static_cast<OutputConfig
*>(w
->innerWidget());
144 RandROutput
*output
= config
->output();
146 if(!output
->isConnected())
149 QSize res
= config
->resolution();
152 if(!config
->hasPendingChanges()) {
153 kDebug() << "Ignoring identical config for" << output
->name();
156 QRect
configuredRect(config
->position(), res
);
158 kDebug() << "Output config for" << output
->name() << ":\n"
159 " rect =" << configuredRect
160 << ", rot =" << config
->rotation()
161 << ", rate =" << config
->refreshRate();
163 output
->proposeRect(configuredRect
);
164 output
->proposeRotation(config
->rotation());
165 output
->proposeRefreshRate(config
->refreshRate());
166 } else { // user wants to disable this output
167 kDebug() << "Disabling" << output
->name();
168 output
->slotDisable();
171 m_display
->applyProposed();
175 void RandRConfig::slotChanged(void)
182 void RandRConfig::update()
189 bool RandRConfig::eventFilter(QObject
*obj
, QEvent
*event
)
191 if ( obj
== screenView
&& event
->type() == QEvent::Resize
) {
195 return QWidget::eventFilter(obj
, event
);
199 void RandRConfig::slotAdjustOutput(OutputGraphicsItem
*o
)
202 kDebug() << "Output graphics item changed:";
207 void RandRConfig::slotUpdateView()
212 // updates the graphics view so that all outputs fit inside of it
213 OutputMap outputs
= m_display
->currentScreen()->outputs();
214 foreach(RandROutput
*output
, outputs
)
222 r
= r
.united(output
->rect());
224 // scale the total bounding rectangle for all outputs to fit
225 // 80% of the containing QGraphicsView
226 float scaleX
= (float)screenView
->width() / r
.width();
227 float scaleY
= (float)screenView
->height() / r
.height();
228 float scale
= (scaleX
< scaleY
) ? scaleX
: scaleY
;
231 screenView
->resetMatrix();
232 screenView
->scale(scale
,scale
);
233 screenView
->ensureVisible(r
);
234 screenView
->setSceneRect(r
);
237 uint
qHash( const QPoint
& p
)
239 return p
.x() * 10000 + p
.y();
242 void RandRConfig::identifyOutputs()
244 identifyTimer
.stop();
246 QHash
< QPoint
, QStringList
> ids
; // outputs at centers of screens (can be more in case of clone mode)
247 OutputMap outputs
= m_display
->currentScreen()->outputs();
248 foreach(RandROutput
*output
, outputs
)
250 if( !output
->isConnected())
252 ids
[ output
->rect().center() ].append( output
->name());
254 for( QHash
< QPoint
, QStringList
>::ConstIterator it
= ids
.constBegin();
255 it
!= ids
.constEnd();
258 QLabel
*si
= new QLabel(it
->join("\n"), NULL
, Qt::X11BypassWindowManagerHint
);
259 QFont fnt
= KGlobalSettings::generalFont();
260 fnt
.setPixelSize(100);
262 si
->setFrameStyle(QFrame::Panel
);
263 si
->setFrameShadow(QFrame::Plain
);
264 si
->setAlignment(Qt::AlignCenter
);
265 QRect
targetGeometry(QPoint(0,0), si
->sizeHint());
266 targetGeometry
.moveCenter(it
.key());
267 si
->setGeometry(targetGeometry
);
269 m_indicators
.append( si
);
271 identifyTimer
.start( 1500 );
274 void RandRConfig::clearIndicators()
276 qDeleteAll( m_indicators
);
277 m_indicators
.clear();
280 #include "randrconfig.moc"