TDE base libraries and programs
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

221 lines
6.9 KiB

  1. /* vi: ts=8 sts=4 sw=4
  2. kate: space-indent on; indent-width 4; indent-mode cstyle;
  3. This file is part of the KDE project, module kcmbackground.
  4. Copyright (C) 2002 Laurent Montel <montell@club-internet.fr>
  5. Copyright (C) 2003 Waldo Bastian <bastian@kde.org>
  6. Copyright (C) 2005 David Saxton <david@bluehaze.org>
  7. This program is free software; you can redistribute it and/or
  8. modify it under the terms of the GNU General Public License
  9. version 2 as published by the Free Software Foundation.
  10. This library 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 GNU
  13. Library General Public License for more details.
  14. You should have received a copy of the GNU Library General Public License
  15. along with this library; see the file COPYING.LIB. If not, write to
  16. the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  17. Boston, MA 02110-1301, USA.
  18. */
  19. #include <kdebug.h>
  20. #include <tdelocale.h>
  21. #include <kpixmap.h>
  22. #include <kstandarddirs.h>
  23. #include <kurldrag.h>
  24. #include <tqapplication.h>
  25. #include <tqpixmap.h>
  26. #include <tqwhatsthis.h>
  27. #include "bgmonitor.h"
  28. // Constants used (should they be placed somewhere?)
  29. // Size of monitor image: 200x186
  30. // Geometry of "display" part of monitor image: (23,14)-[151x115]
  31. //BEGIN class BGMonitorArrangement
  32. BGMonitorArrangement::BGMonitorArrangement(TQWidget *parent, const char *name)
  33. : TQWidget(parent, name)
  34. {
  35. m_pBGMonitor.resize( TQApplication::desktop()->numScreens(), 0l );
  36. for (int screen = 0; screen < TQApplication::desktop()->numScreens(); ++screen)
  37. {
  38. BGMonitorLabel * label = new BGMonitorLabel(this);
  39. m_pBGMonitor[screen] = label;
  40. connect( label->monitor(), TQT_SIGNAL(imageDropped(const TQString &)), this, TQT_SIGNAL(imageDropped(const TQString &)) );
  41. }
  42. parent->setFixedSize(200, 186);
  43. setFixedSize(200, 186);
  44. updateArrangement();
  45. }
  46. BGMonitor * BGMonitorArrangement::monitor( unsigned screen ) const
  47. {
  48. return m_pBGMonitor[screen]->monitor();
  49. }
  50. TQRect BGMonitorArrangement::expandToPreview( TQRect r ) const
  51. {
  52. double scaleX = 200.0 / 151.0;
  53. double scaleY = 186.0 / 115.0;
  54. return TQRect( int(r.x()*scaleX), int(r.y()*scaleY), int(r.width()*scaleX), int(r.height()*scaleY) );
  55. }
  56. TQSize BGMonitorArrangement::expandToPreview( TQSize s ) const
  57. {
  58. double scaleX = 200.0 / 151.0;
  59. double scaleY = 186.0 / 115.0;
  60. return TQSize( int(s.width()*scaleX), int(s.height()*scaleY) );
  61. }
  62. TQPoint BGMonitorArrangement::expandToPreview( TQPoint p ) const
  63. {
  64. double scaleX = 200.0 / 151.0;
  65. double scaleY = 186.0 / 115.0;
  66. return TQPoint( int(p.x()*scaleX), int(p.y()*scaleY) );
  67. }
  68. void BGMonitorArrangement::updateArrangement()
  69. {
  70. // In this function, sizes, etc have a normal value, and their "expanded"
  71. // value. The expanded value is used for setting the size of the monitor
  72. // image that contains the preview of the background. The monitor image
  73. // will set the background preview back to the normal value.
  74. TQRect overallGeometry;
  75. for (int screen = 0; screen < TQApplication::desktop()->numScreens(); ++screen)
  76. overallGeometry |= TQApplication::desktop()->screenGeometry(screen);
  77. TQRect expandedOverallGeometry = expandToPreview(overallGeometry);
  78. double scale = TQMIN(
  79. double(width()) / double(expandedOverallGeometry.width()),
  80. double(height()) / double(expandedOverallGeometry.height())
  81. );
  82. m_combinedPreviewSize = overallGeometry.size() * scale;
  83. m_maxPreviewSize = TQSize(0,0);
  84. int previousMax = 0;
  85. for (int screen = 0; screen < TQApplication::desktop()->numScreens(); ++screen)
  86. {
  87. TQPoint topLeft = (TQApplication::desktop()->screenGeometry(screen).topLeft() - overallGeometry.topLeft()) * scale;
  88. TQPoint expandedTopLeft = expandToPreview(topLeft);
  89. TQSize previewSize = TQApplication::desktop()->screenGeometry(screen).size() * scale;
  90. TQSize expandedPreviewSize = expandToPreview(previewSize);
  91. if ( (previewSize.width() * previewSize.height()) > previousMax )
  92. {
  93. previousMax = previewSize.width() * previewSize.height();
  94. m_maxPreviewSize = previewSize;
  95. }
  96. m_pBGMonitor[screen]->setPreviewPosition( TQRect( topLeft, previewSize ) );
  97. m_pBGMonitor[screen]->setGeometry( TQRect( expandedTopLeft, expandedPreviewSize ) );
  98. m_pBGMonitor[screen]->updateMonitorGeometry();
  99. }
  100. }
  101. void BGMonitorArrangement::resizeEvent( TQResizeEvent * e )
  102. {
  103. TQWidget::resizeEvent(e);
  104. updateArrangement();
  105. }
  106. void BGMonitorArrangement::setPixmap( const KPixmap & pm )
  107. {
  108. for (unsigned screen = 0; screen < m_pBGMonitor.size(); ++screen)
  109. {
  110. TQRect position = m_pBGMonitor[screen]->previewPosition();
  111. TQPixmap monitorPixmap( position.size(), pm.depth() );
  112. copyBlt( &monitorPixmap, 0, 0, &pm, position.x(), position.y(), position.width(), position.height() );
  113. m_pBGMonitor[screen]->monitor()->setPixmap(monitorPixmap);
  114. }
  115. }
  116. //END class BGMonitorArrangement
  117. //BEGIN class BGMonitorLabel
  118. BGMonitorLabel::BGMonitorLabel(TQWidget *parent, const char *name)
  119. : TQLabel(parent, name)
  120. {
  121. setAlignment(AlignCenter);
  122. setScaledContents(true);
  123. setPixmap( TQPixmap( locate("data", "kcontrol/pics/monitor.png") ) );
  124. m_pBGMonitor = new BGMonitor(this);
  125. TQWhatsThis::add( this, i18n("This picture of a monitor contains a preview of what the current settings will look like on your desktop.") );
  126. }
  127. void BGMonitorLabel::updateMonitorGeometry()
  128. {
  129. double scaleX = double(width()) / double(sizeHint().width());
  130. double scaleY = double(height()) / double(sizeHint().height());
  131. kdDebug() << k_funcinfo << " Setting geometry to " << TQRect( int(23*scaleX), int(14*scaleY), int(151*scaleX), int(115*scaleY) ) << endl;
  132. m_pBGMonitor->setGeometry( int(23*scaleX), int(14*scaleY), int(151*scaleX), int(115*scaleY) );
  133. }
  134. void BGMonitorLabel::resizeEvent( TQResizeEvent * e )
  135. {
  136. TQWidget::resizeEvent(e);
  137. updateMonitorGeometry();
  138. }
  139. //END class BGMonitorLabel
  140. //BEGIN class BGMonitor
  141. BGMonitor::BGMonitor(TQWidget *parent, const char *name)
  142. : TQLabel(parent, name)
  143. {
  144. setAlignment(AlignCenter);
  145. setScaledContents(true);
  146. setAcceptDrops(true);
  147. }
  148. void BGMonitor::dropEvent(TQDropEvent *e)
  149. {
  150. if (!KURLDrag::canDecode(e))
  151. return;
  152. KURL::List uris;
  153. if (KURLDrag::decode(e, uris) && (uris.count() > 0)) {
  154. // TODO: Download remote file
  155. if (uris.first().isLocalFile())
  156. emit imageDropped(uris.first().path());
  157. }
  158. }
  159. void BGMonitor::dragEnterEvent(TQDragEnterEvent *e)
  160. {
  161. if (KURLDrag::canDecode(e))
  162. e->accept(rect());
  163. else
  164. e->ignore(rect());
  165. }
  166. //END class BGMonitor
  167. #include "bgmonitor.moc"