summaryrefslogtreecommitdiffstats
path: root/kcontrol/background/bgmonitor.cpp
blob: 3c2278722631dde9849a7534be0a55678519589a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/* vi: ts=8 sts=4 sw=4
   kate: space-indent on; indent-width 4; indent-mode cstyle;

   This file is part of the KDE project, module kcmbackground.

   Copyright (C) 2002 Laurent Montel <montell@club-internet.fr>
   Copyright (C) 2003 Waldo Bastian <bastian@kde.org>
   Copyright (C) 2005 David Saxton <david@bluehaze.org>
  
   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License 
   version 2 as published by the Free Software Foundation.
   
   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.
   
   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
 */

#include <kdebug.h>
#include <klocale.h>
#include <kpixmap.h>
#include <kstandarddirs.h>
#include <kurldrag.h>
#include <tqapplication.h>
#include <tqpixmap.h>
#include <tqwhatsthis.h>

#include "bgmonitor.h"

// Constants used (should they be placed somewhere?)
// Size of monitor image: 200x186
// Geometry of "display" part of monitor image: (23,14)-[151x115]

//BEGIN class BGMonitorArrangement
BGMonitorArrangement::BGMonitorArrangement(TQWidget *parent, const char *name)
    : TQWidget(parent, name)
{
    m_pBGMonitor.resize( TQApplication::desktop()->numScreens(), 0l );
    
    for (int screen = 0; screen < TQApplication::desktop()->numScreens(); ++screen)
    {
        BGMonitorLabel * label = new BGMonitorLabel(this);
        m_pBGMonitor[screen] = label;
        
        connect( label->monitor(), TQT_SIGNAL(imageDropped(const TQString &)), this, TQT_SIGNAL(imageDropped(const TQString &)) );
    }
    
    parent->setFixedSize(200, 186);
    setFixedSize(200, 186);
    updateArrangement();
}


BGMonitor * BGMonitorArrangement::monitor( unsigned screen ) const
{
    return m_pBGMonitor[screen]->monitor();
}


TQRect BGMonitorArrangement::expandToPreview( TQRect r ) const
{
    double scaleX = 200.0 / 151.0;
    double scaleY = 186.0 / 115.0;
    return TQRect( int(r.x()*scaleX), int(r.y()*scaleY), int(r.width()*scaleX), int(r.height()*scaleY) );
}


TQSize BGMonitorArrangement::expandToPreview( TQSize s ) const
{
    double scaleX = 200.0 / 151.0;
    double scaleY = 186.0 / 115.0;
    return TQSize( int(s.width()*scaleX), int(s.height()*scaleY) );
}


TQPoint BGMonitorArrangement::expandToPreview( TQPoint p ) const
{
    double scaleX = 200.0 / 151.0;
    double scaleY = 186.0 / 115.0;
    return TQPoint( int(p.x()*scaleX), int(p.y()*scaleY) );
}


void BGMonitorArrangement::updateArrangement()
{
    // In this function, sizes, etc have a normal value, and their "expanded"
    // value. The expanded value is used for setting the size of the monitor
    // image that tqcontains the preview of the background. The monitor image
    // will set the background preview back to the normal value.
    
    TQRect overallGeometry;
    for (int screen = 0; screen < TQApplication::desktop()->numScreens(); ++screen)
        overallGeometry |= TQApplication::desktop()->screenGeometry(screen);
    
    TQRect expandedOverallGeometry = expandToPreview(overallGeometry);
    
    double scale = QMIN(
                double(width()) / double(expandedOverallGeometry.width()),
                double(height()) / double(expandedOverallGeometry.height())
                       );
    
    m_combinedPreviewSize = overallGeometry.size() * scale;
    
    m_maxPreviewSize = TQSize(0,0);
    int previousMax = 0;
    
    for (int screen = 0; screen < TQApplication::desktop()->numScreens(); ++screen)
    {
        TQPoint topLeft = (TQApplication::desktop()->screenGeometry(screen).topLeft() - overallGeometry.topLeft()) * scale;
        TQPoint expandedTopLeft = expandToPreview(topLeft);
        
        TQSize previewSize = TQApplication::desktop()->screenGeometry(screen).size() * scale;
        TQSize expandedPreviewSize = expandToPreview(previewSize);
        
        if ( (previewSize.width() * previewSize.height()) > previousMax )
        {
            previousMax = previewSize.width() * previewSize.height();
            m_maxPreviewSize = previewSize;
        }
        
        m_pBGMonitor[screen]->setPreviewPosition( TQRect( topLeft, previewSize ) );
        m_pBGMonitor[screen]->setGeometry( TQRect( expandedTopLeft, expandedPreviewSize ) );
        m_pBGMonitor[screen]->updateMonitorGeometry();
    }
}


void BGMonitorArrangement::resizeEvent( TQResizeEvent * e )
{
    TQWidget::resizeEvent(e);
    updateArrangement();
}


void BGMonitorArrangement::setPixmap( const KPixmap & pm )
{
    for (unsigned screen = 0; screen < m_pBGMonitor.size(); ++screen)
    {
        TQRect position = m_pBGMonitor[screen]->previewPosition();
        
        TQPixmap monitorPixmap( position.size(), pm.depth() );
        copyBlt( &monitorPixmap, 0, 0, &pm, position.x(), position.y(), position.width(), position.height() );
        m_pBGMonitor[screen]->monitor()->setPixmap(monitorPixmap);
    }
}
//END class BGMonitorArrangement



//BEGIN class BGMonitorLabel
BGMonitorLabel::BGMonitorLabel(TQWidget *parent, const char *name)
    : TQLabel(parent, name)
{
    tqsetAlignment(AlignCenter);
    setScaledContents(true);
    setPixmap( TQPixmap( locate("data",  "kcontrol/pics/monitor.png") ) );
    m_pBGMonitor = new BGMonitor(this);
    
    TQWhatsThis::add( this, i18n("This picture of a monitor tqcontains a preview of what the current settings will look like on your desktop.") );
}


void BGMonitorLabel::updateMonitorGeometry()
{
    double scaleX = double(width()) / double(tqsizeHint().width());
    double scaleY = double(height()) / double(tqsizeHint().height());
    
    kdDebug() << k_funcinfo << " Setting tqgeometry to " << TQRect( int(23*scaleX), int(14*scaleY), int(151*scaleX), int(115*scaleY) ) << endl;
    m_pBGMonitor->setGeometry( int(23*scaleX), int(14*scaleY), int(151*scaleX), int(115*scaleY) );
}


void BGMonitorLabel::resizeEvent( TQResizeEvent * e )
{
    TQWidget::resizeEvent(e);
    updateMonitorGeometry();
}
//END class BGMonitorLabel



//BEGIN class BGMonitor
BGMonitor::BGMonitor(TQWidget *parent, const char *name)
    : TQLabel(parent, name)
{
    tqsetAlignment(AlignCenter);
    setScaledContents(true);
    setAcceptDrops(true);
}


void BGMonitor::dropEvent(TQDropEvent *e)
{
    if (!KURLDrag::canDecode(e))
        return;

    KURL::List uris;
    if (KURLDrag::decode(e, uris) && (uris.count() > 0)) {
        // TODO: Download remote file
        if (uris.first().isLocalFile())
           emit imageDropped(uris.first().path());
    }
}

void BGMonitor::dragEnterEvent(TQDragEnterEvent *e)
{
    if (KURLDrag::canDecode(e))
        e->accept(rect());
    else
        e->ignore(rect());
}
//END class BGMonitor

#include "bgmonitor.moc"