Turn songdb into a plugin, lots of tweaks, drag'n'drop of songs temporarily broken
[kworship.git] / kworship / DesktopView.cpp
bloba53a1e140262084fa25e401375629054b6cab6e3
1 /***************************************************************************
2 * This file is part of KWorship. *
3 * Copyright 2008 James Hogan <james@albanarts.com> *
4 * *
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. *
9 * *
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. *
14 * *
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 ***************************************************************************/
20 /**
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>
34 #include <cassert>
37 * Constructors + destructor
40 /// Primary constructor.
41 DesktopView::DesktopView(QWidget* parent)
42 : QGraphicsView(parent)
43 , m_desktopRect()
44 , m_multiSelect(false)
45 , m_noSelect(true)
46 , m_selectedScreen(-1)
47 , m_selectedScreens()
49 setup();
52 /// Destructor.
53 DesktopView::~DesktopView()
58 * Main interface
61 /// Find whether a screen is selected.
62 bool DesktopView::isScreenSelected(int screen) const
64 assert(screen >= 0 && screen < m_selectedScreens.size());
65 if (m_multiSelect)
67 return m_selectedScreens[screen];
69 else
71 return (screen == m_selectedScreen);
75 /// Find which single screen is selected.
76 int DesktopView::selectedScreen() const
78 if (m_multiSelect)
80 /// @todo Implement multiselect selectedScreen()
81 return -1;
83 else
85 return m_selectedScreen;
90 * Public slots
93 /// Set the selected screen.
94 void DesktopView::setSelectedScreen(int selectedScreen)
96 if (m_multiSelect)
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();
106 setup();
111 * Events
114 void DesktopView::resizeEvent(QResizeEvent*)
116 resetMatrix();
117 fitInView(m_desktopRect, Qt::KeepAspectRatio);
120 void DesktopView::mousePressEvent(QMouseEvent* event)
122 if (isEnabled())
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());
131 if (m_multiSelect)
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]);
138 else
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();
157 else
159 if (m_multiSelect)
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();
176 setup();
178 QGraphicsView::mousePressEvent(event);
182 * Private slots
185 /// Indicates that a screen has been resized.
186 void DesktopView::resizeScreen(int screen)
188 Q_UNUSED(screen)
189 setup();
193 * Private functions
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
206 delete scene();
208 int oldSize = m_selectedScreens.size();
209 m_selectedScreens.resize(screens);
210 if (m_multiSelect)
212 while (oldSize < screens)
214 m_selectedScreens[oldSize] = false;
215 ++oldSize;
219 // Set up the screens preview
220 QGraphicsScene* scene = new QGraphicsScene(this);
221 setScene(scene);
223 for (int i = 0; i < screens; ++i)
225 QRectF geom = desktop->screenGeometry(i);
227 // rectangle
228 QGraphicsRectItem* rect = scene->addRect(geom, QPen(Qt::black),
229 isScreenSelected(i) ? Qt::blue :
230 QBrush((primary == i) ? Qt::gray :
231 Qt::lightGray));
233 // text
234 QString string;
235 string.setNum(i+1);
236 QGraphicsTextItem* text = new QGraphicsTextItem(string, rect);
238 QFont font;
239 font.setPixelSize(geom.height() * 0.8f);
240 text->setFont(font);
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();
247 resizeEvent(0);
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)
274 .arg(geom.width())
275 .arg(geom.height()));
277 else
279 statusChanged(tr("Screen %1: not plugged in.")
280 .arg(m_selectedScreen+1));
283 else
285 statusChanged("");
287 screenChanged(m_selectedScreen);