1 /***************************************************************************
2 * This file is part of KWorship. *
3 * Copyright 2008 James Hogan <james@albanarts.com> *
5 * KWorship 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 * KWorship 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 KWorship. If not, write to the Free Software Foundation, *
17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ***************************************************************************/
21 * @file DesktopView.cpp
22 * @brief Preview of the screens on the desktop.
23 * @author James Hogan <james@albanarts.com>
26 #include "DesktopView.h"
28 #include <QApplication>
29 #include <QDesktopWidget>
30 #include <QGraphicsRectItem>
31 #include <QGraphicsTextItem>
32 #include <QMouseEvent>
37 * Constructors + destructor
40 /// Primary constructor.
41 DesktopView::DesktopView(QWidget
* parent
)
42 : QGraphicsView(parent
)
44 , m_multiSelect(false)
46 , m_selectedScreen(-1)
53 DesktopView::~DesktopView()
61 /// Find whether a screen is selected.
62 bool DesktopView::isScreenSelected(int screen
) const
64 assert(screen
>= 0 && screen
< m_selectedScreens
.size());
67 return m_selectedScreens
[screen
];
71 return (screen
== m_selectedScreen
);
75 /// Find which single screen is selected.
76 int DesktopView::selectedScreen() const
80 /// @todo Implement multiselect selectedScreen()
85 return m_selectedScreen
;
93 /// Set the selected screen.
94 void DesktopView::setSelectedScreen(int selectedScreen
)
98 /// @todo Implement multiselect setSelectedScreen(int)
100 else if (m_selectedScreen
!= selectedScreen
)
102 screenSelected(m_selectedScreen
, false);
103 m_selectedScreen
= selectedScreen
;
104 screenSelected(m_selectedScreen
, true);
105 singleSelectRestatus();
114 void DesktopView::resizeEvent(QResizeEvent
*)
117 fitInView(m_desktopRect
, Qt::KeepAspectRatio
);
120 void DesktopView::mousePressEvent(QMouseEvent
* event
)
124 QDesktopWidget
* desktop
= qobject_cast
<QApplication
*>(QCoreApplication::instance())->desktop();
125 if (0 != itemAt(event
->pos()))
127 QPoint pos
= mapToScene(event
->pos()).toPoint();
128 int screen
= desktop
->screenNumber(pos
);
129 assert(screen
>= 0 && screen
< m_selectedScreens
.size());
133 if (event
->modifiers().testFlag(Qt::ControlModifier
))
135 // We know its changed here so no need to use selectScreen
136 screenSelected(screen
, m_selectedScreens
[screen
] = !m_selectedScreens
[screen
]);
140 for (int i
= 0; i
< m_selectedScreens
.size(); ++i
)
142 selectScreen(i
, i
== screen
);
146 else if (m_selectedScreen
!= screen
)
148 if (m_selectedScreen
>= 0)
150 screenSelected(m_selectedScreen
, false);
152 m_selectedScreen
= screen
;
153 screenSelected(m_selectedScreen
, true);
154 singleSelectRestatus();
161 if (!event
->modifiers().testFlag(Qt::ControlModifier
))
163 for (int i
= 0; i
< m_selectedScreens
.size(); ++i
)
165 selectScreen(i
, false);
169 else if (m_noSelect
&& m_selectedScreen
>= 0)
171 screenSelected(m_selectedScreen
, false);
172 m_selectedScreen
= -1;
173 singleSelectRestatus();
178 QGraphicsView::mousePressEvent(event
);
185 /// Indicates that a screen has been resized.
186 void DesktopView::resizeScreen(int screen
)
196 /// Setup the display.
197 void DesktopView::setup()
199 QDesktopWidget
* desktop
= qobject_cast
<QApplication
*>(QCoreApplication::instance())->desktop();
200 assert(0 != desktop
);
201 int screens
= desktop
->numScreens();
202 assert(0 != screens
);
203 int primary
= desktop
->primaryScreen();
205 // Cleanup any previous scene
208 int oldSize
= m_selectedScreens
.size();
209 m_selectedScreens
.resize(screens
);
212 while (oldSize
< screens
)
214 m_selectedScreens
[oldSize
] = false;
219 // Set up the screens preview
220 QGraphicsScene
* scene
= new QGraphicsScene(this);
223 for (int i
= 0; i
< screens
; ++i
)
225 QRectF geom
= desktop
->screenGeometry(i
);
228 QGraphicsRectItem
* rect
= scene
->addRect(geom
, QPen(Qt::black
),
229 isScreenSelected(i
) ? Qt::blue
:
230 QBrush((primary
== i
) ? Qt::gray
:
236 QGraphicsTextItem
* text
= new QGraphicsTextItem(string
, rect
);
239 font
.setPixelSize(geom
.height() * 0.8f
);
242 QRectF textRect
= text
->boundingRect();
243 text
->setPos(QPointF(geom
.x() + (geom
.width() - textRect
.width()) / 2,
244 geom
.y() + (geom
.height() - textRect
.height()) / 2));
246 m_desktopRect
= desktop
->rect();
250 /// Change selection of a screen.
251 void DesktopView::selectScreen(int screen
, bool newSelected
)
253 assert(m_multiSelect
);
254 assert(screen
>= 0 && screen
< m_selectedScreens
.size());
255 if (m_selectedScreens
[screen
] != newSelected
)
257 screenSelected(screen
, m_selectedScreens
[screen
] = newSelected
);
261 /// Update a single selection status.
262 void DesktopView::singleSelectRestatus()
264 assert(!m_multiSelect
);
265 if (m_selectedScreen
>= 0)
267 QDesktopWidget
* desktop
= qobject_cast
<QApplication
*>(QCoreApplication::instance())->desktop();
268 assert(0 != desktop
);
269 if (m_selectedScreen
< m_selectedScreens
.size())
271 QRectF geom
= desktop
->screenGeometry(m_selectedScreen
);
272 statusChanged(tr("Screen %1: \%2x%3.")
273 .arg(m_selectedScreen
+1)
275 .arg(geom
.height()));
279 statusChanged(tr("Screen %1: not plugged in.")
280 .arg(m_selectedScreen
+1));
287 screenChanged(m_selectedScreen
);