not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / kcontrol / kdm / background / bgmonitor.cpp
blob4f4942a8fd0facb6ee27f1f26e413084b01c5993
1 /* vi: ts=8 sts=4 sw=4
2 kate: space-indent on; indent-width 4; indent-mode cstyle;
4 This file is part of the KDE project, module kcmbackground.
6 Copyright (C) 2002 Laurent Montel <montell@club-internet.fr>
7 Copyright (C) 2003 Waldo Bastian <bastian@kde.org>
8 Copyright (C) 2005 David Saxton <david@bluehaze.org>
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License
12 version 2 as published by the Free Software Foundation.
14 This library is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Library General Public License for more details.
19 You should have received a copy of the GNU Library General Public License
20 along with this library; see the file COPYING.LIB. If not, write to
21 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 Boston, MA 02110-1301, USA.
25 #include <k3urldrag.h>
27 #include "bgmonitor.h"
28 //Added by qt3to4:
29 #include <QDragEnterEvent>
30 #include <QDropEvent>
31 #include <QApplication>
32 #include <QDesktopWidget>
33 #include <QPixmap>
34 #include <klocale.h>
35 #include <kdebug.h>
36 #include <kstandarddirs.h>
37 #include <QPainter>
39 // Constants used (should they be placed somewhere?)
40 // Size of monitor image: 200x186
41 // Geometry of "display" part of monitor image: (23,14)-[151x115]
43 //BEGIN class BGMonitorArrangement
44 BGMonitorArrangement::BGMonitorArrangement(QWidget *parent)
45 : QWidget(parent)
47 m_pBGMonitor.resize( QApplication::desktop()->numScreens());
49 for (int screen = 0; screen < QApplication::desktop()->numScreens(); ++screen)
51 BGMonitorLabel * label = new BGMonitorLabel(this);
52 m_pBGMonitor[screen] = label;
54 connect( label->monitor(), SIGNAL(imageDropped(const QString &)), this, SIGNAL(imageDropped(const QString &)) );
57 parent->setFixedSize(200, 186);
58 setFixedSize(200, 186);
59 updateArrangement();
63 BGMonitor * BGMonitorArrangement::monitor( unsigned screen ) const
65 return m_pBGMonitor[screen]->monitor();
69 QRect BGMonitorArrangement::expandToPreview( const QRect &r ) const
71 double scaleX = 200.0 / 151.0;
72 double scaleY = 186.0 / 115.0;
73 return QRect( int(r.x()*scaleX), int(r.y()*scaleY), int(r.width()*scaleX), int(r.height()*scaleY) );
77 QSize BGMonitorArrangement::expandToPreview( const QSize &s ) const
79 double scaleX = 200.0 / 151.0;
80 double scaleY = 186.0 / 115.0;
81 return QSize( int(s.width()*scaleX), int(s.height()*scaleY) );
85 QPoint BGMonitorArrangement::expandToPreview( const QPoint &p ) const
87 double scaleX = 200.0 / 151.0;
88 double scaleY = 186.0 / 115.0;
89 return QPoint( int(p.x()*scaleX), int(p.y()*scaleY) );
93 void BGMonitorArrangement::updateArrangement()
95 // In this function, sizes, etc have a normal value, and their "expanded"
96 // value. The expanded value is used for setting the size of the monitor
97 // image that contains the preview of the background. The monitor image
98 // will set the background preview back to the normal value.
100 QRect overallGeometry;
101 for (int screen = 0; screen < QApplication::desktop()->numScreens(); ++screen)
102 overallGeometry |= QApplication::desktop()->screenGeometry(screen);
104 QRect expandedOverallGeometry = expandToPreview(overallGeometry);
106 double scale = qMin(
107 double(width()) / double(expandedOverallGeometry.width()),
108 double(height()) / double(expandedOverallGeometry.height())
111 m_combinedPreviewSize = overallGeometry.size() * scale;
113 m_maxPreviewSize = QSize(0,0);
114 int previousMax = 0;
116 for (int screen = 0; screen < QApplication::desktop()->numScreens(); ++screen)
118 QPoint topLeft = (QApplication::desktop()->screenGeometry(screen).topLeft() - overallGeometry.topLeft()) * scale;
119 QPoint expandedTopLeft = expandToPreview(topLeft);
121 QSize previewSize = QApplication::desktop()->screenGeometry(screen).size() * scale;
122 QSize expandedPreviewSize = expandToPreview(previewSize);
124 if ( (previewSize.width() * previewSize.height()) > previousMax )
126 previousMax = previewSize.width() * previewSize.height();
127 m_maxPreviewSize = previewSize;
130 m_pBGMonitor[screen]->setPreviewPosition( QRect( topLeft, previewSize ) );
131 m_pBGMonitor[screen]->setGeometry( QRect( expandedTopLeft, expandedPreviewSize ) );
132 m_pBGMonitor[screen]->updateMonitorGeometry();
137 void BGMonitorArrangement::resizeEvent( QResizeEvent * e )
139 QWidget::resizeEvent(e);
140 updateArrangement();
144 void BGMonitorArrangement::setPixmap( const QPixmap & pm )
146 for (int screen = 0; screen < m_pBGMonitor.size(); ++screen)
148 QRect position = m_pBGMonitor[screen]->previewPosition();
150 QPixmap monitorPixmap( position.size());
151 QPainter p(&monitorPixmap);
152 p.drawPixmap(0, 0, pm, position.x(), position.y(), position.width(), position.height() );
153 m_pBGMonitor[screen]->monitor()->setPixmap(monitorPixmap);
156 //END class BGMonitorArrangement
160 //BEGIN class BGMonitorLabel
161 BGMonitorLabel::BGMonitorLabel(QWidget *parent, const char *name)
162 : QLabel(parent)
164 setObjectName( name );
165 setAlignment(Qt::AlignCenter);
166 setScaledContents(true);
167 setPixmap( QPixmap( KStandardDirs::locate("data", "kcontrol/pics/monitor.png") ) );
168 m_pBGMonitor = new BGMonitor(this);
169 setWhatsThis( i18n("This picture of a monitor contains a preview of what the current settings will look like on your desktop.") );
173 void BGMonitorLabel::updateMonitorGeometry()
175 double scaleX = double(width()) / double(sizeHint().width());
176 double scaleY = double(height()) / double(sizeHint().height());
178 kDebug() << " Setting geometry to " << QRect( int(23*scaleX), int(14*scaleY), int(151*scaleX), int(115*scaleY) );
179 m_pBGMonitor->setGeometry( int(23*scaleX), int(14*scaleY), int(151*scaleX), int(115*scaleY) );
183 void BGMonitorLabel::resizeEvent( QResizeEvent * e )
185 QWidget::resizeEvent(e);
186 updateMonitorGeometry();
188 //END class BGMonitorLabel
192 //BEGIN class BGMonitor
193 BGMonitor::BGMonitor(QWidget *parent, const char *name)
194 : QLabel(parent)
196 setObjectName( name );
197 setAlignment(Qt::AlignCenter);
198 setScaledContents(true);
199 setAcceptDrops(true);
203 void BGMonitor::dropEvent(QDropEvent *e)
205 if (!K3URLDrag::canDecode(e))
206 return;
208 KUrl::List uris;
209 if (K3URLDrag::decode(e, uris) && (uris.count() > 0)) {
210 // TODO: Download remote file
211 if (uris.first().isLocalFile())
212 emit imageDropped(uris.first().path());
216 void BGMonitor::dragEnterEvent(QDragEnterEvent *e)
218 if (K3URLDrag::canDecode(e))
219 e->accept(rect());
220 else
221 e->ignore(rect());
223 //END class BGMonitor
225 #include "bgmonitor.moc"