~ubuntu-branches/ubuntu/gutsy/kdebase-workspace/gutsy-backports

« back to all changes in this revision

Viewing changes to kicker/libkicker/utils.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2007-09-05 20:45:14 UTC
  • Revision ID: james.westby@ubuntu.com-20070905204514-632hhspl0nvrc84i
Tags: upstream-3.93.0
ImportĀ upstreamĀ versionĀ 3.93.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*****************************************************************
 
2
 
 
3
Copyright (c) 1996-2000 the kicker authors. See file AUTHORS.
 
4
 
 
5
Permission is hereby granted, free of charge, to any person obtaining a copy
 
6
of this software and associated documentation files (the "Software"), to deal
 
7
in the Software without restriction, including without limitation the rights
 
8
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 
9
copies of the Software, and to permit persons to whom the Software is
 
10
furnished to do so, subject to the following conditions:
 
11
 
 
12
The above copyright notice and this permission notice shall be included in
 
13
all copies or substantial portions of the Software.
 
14
 
 
15
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
16
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
17
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
 
18
AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
 
19
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 
20
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
21
 
 
22
******************************************************************/
 
23
 
 
24
#include <QApplication>
 
25
#include <QFile>
 
26
#include <QRegExp>
 
27
#include <QDesktopWidget>
 
28
#include <QMenuItem>
 
29
#include <QMenu>
 
30
 
 
31
#include <kiconeffect.h>
 
32
#include <kiconloader.h>
 
33
#include <kio/netaccess.h>
 
34
#include <kstandarddirs.h>
 
35
#include <kservice.h>
 
36
 
 
37
#include "utils.h"
 
38
#include "kickerSettings.h"
 
39
 
 
40
namespace Plasma
 
41
{
 
42
 
 
43
Position arrowToDirection(Qt::ArrowType p)
 
44
{
 
45
    switch (p)
 
46
    {
 
47
        case Qt::DownArrow:
 
48
            return Plasma::Down;
 
49
 
 
50
        case Qt::LeftArrow:
 
51
            return Plasma::Left;
 
52
 
 
53
        case Qt::RightArrow:
 
54
            return Plasma::Right;
 
55
 
 
56
        case Qt::UpArrow:
 
57
        default:
 
58
            return Plasma::Up;
 
59
    }
 
60
}
 
61
 
 
62
Position popupDirectionForPosition(Position p)
 
63
{
 
64
    switch (p)
 
65
    {
 
66
        case Left:
 
67
            return Right;
 
68
 
 
69
        case Right:
 
70
            return Left;
 
71
 
 
72
        case Up:
 
73
            return Down;
 
74
 
 
75
        case Down:
 
76
            return Up;
 
77
 
 
78
        case Floating:
 
79
            return Right; // FIXME?
 
80
    }
 
81
        return Right; //FIXME ?
 
82
}
 
83
 
 
84
int sizeValue(Plasma::Size s)
 
85
{
 
86
    switch (s)
 
87
    {
 
88
        case Plasma::SizeTiny:
 
89
            return 24;
 
90
 
 
91
        case Plasma::SizeSmall:
 
92
            return 30;
 
93
 
 
94
        case Plasma::SizeNormal:
 
95
            return 46;
 
96
 
 
97
        case Plasma::SizeLarge:
 
98
        default:
 
99
            return 58;
 
100
    }
 
101
}
 
102
 
 
103
int maxButtonDim()
 
104
{
 
105
    return (2 * KickerSettings::iconMargin()) + K3Icon::SizeLarge;
 
106
}
 
107
 
 
108
QString newDesktopFile(const KUrl& url)
 
109
{
 
110
   QString base = url.fileName();
 
111
   if (base.endsWith(".desktop"))
 
112
      base.truncate(base.length()-8);
 
113
   QRegExp r("(.*)(?=-\\d+)");
 
114
   if (r.indexIn(base) > -1)
 
115
      base = r.cap(1);
 
116
 
 
117
   QString file = base + ".desktop";
 
118
 
 
119
   for(int n = 1; ++n; )
 
120
   {
 
121
      QString path = KStandardDirs::locate("appdata", file);
 
122
      if (path.isEmpty())
 
123
         break;
 
124
 
 
125
      file = QString("%2-%1.desktop").arg(n).arg(base);
 
126
   }
 
127
   file = KStandardDirs::locateLocal("appdata", file);
 
128
   return file;
 
129
}
 
130
 
 
131
QString copyDesktopFile(const KUrl& url)
 
132
{
 
133
   QString file = newDesktopFile(url);
 
134
   KUrl dest;
 
135
   dest.setPath(file);
 
136
   KIO::NetAccess::upload(url.path(), dest, 0);
 
137
   return file;
 
138
}
 
139
 
 
140
QMenu* reduceMenu(QMenu *menu)
 
141
{
 
142
    if (menu->actions().count() != 1)
 
143
    {
 
144
       return menu;
 
145
    }
 
146
 
 
147
    QAction *act = menu->actions().value(0);
 
148
 
 
149
    if (act->menu())
 
150
    {
 
151
       return reduceMenu(act->menu());
 
152
    }
 
153
 
 
154
    return menu;
 
155
}
 
156
 
 
157
QPoint popupPosition(Plasma::Position d,
 
158
                     const QWidget* popup,
 
159
                     const QWidget* source,
 
160
                     const QPoint& offset)
 
161
{
 
162
    QRect r;
 
163
    if (source->isTopLevel())
 
164
    {
 
165
        r = source->geometry();
 
166
    }
 
167
    else
 
168
    {
 
169
        r = QRect(source->mapToGlobal(QPoint(0, 0)),
 
170
                  source->mapToGlobal(QPoint(source->width(), source->height())));
 
171
 
 
172
        switch (d)
 
173
        {
 
174
            case Plasma::Left:
 
175
            case Plasma::Right:
 
176
                r.setLeft( source->topLevelWidget()->x() );
 
177
                r.setWidth( source->topLevelWidget()->width() );
 
178
                break;
 
179
            case Plasma::Up:
 
180
            case Plasma::Down:
 
181
                r.setTop( source->topLevelWidget()->y() );
 
182
                r.setHeight( source->topLevelWidget()->height() );
 
183
                break;
 
184
            default:
 
185
                //badbadbad - should never happen
 
186
                break;
 
187
        }
 
188
    }
 
189
 
 
190
    switch (d)
 
191
    {
 
192
        case Plasma::Left:
 
193
        case Plasma::Right:
 
194
        {
 
195
            QDesktopWidget* desktop = QApplication::desktop();
 
196
            QRect screen = desktop->screenGeometry(desktop->screenNumber(const_cast<QWidget*>(source)));
 
197
            int x = (d == Plasma::Left) ? r.left() - popup->width() :
 
198
                                          r.right() + 1;
 
199
            int y = r.top() + offset.y();
 
200
 
 
201
            // try to keep this on screen
 
202
            if (y + popup->height() > screen.bottom())
 
203
            {
 
204
                y = r.bottom() - popup->height() + offset.y();
 
205
 
 
206
                if (y < screen.top())
 
207
                {
 
208
                    y = screen.bottom() - popup->height();
 
209
 
 
210
                    if (y < screen.top())
 
211
                    {
 
212
                        y = screen.top();
 
213
                    }
 
214
                }
 
215
            }
 
216
 
 
217
            return QPoint(x, y);
 
218
        }
 
219
        case Plasma::Up:
 
220
        case Plasma::Down:
 
221
        default:
 
222
        {
 
223
            int x = 0;
 
224
            int y = (d == Plasma::Up) ? r.top() - popup->height() :
 
225
                                        r.bottom() + 1;
 
226
 
 
227
            if (QApplication::isRightToLeft())
 
228
            {
 
229
                x = r.right() - popup->width() + 1;
 
230
 
 
231
                if (offset.x() > 0)
 
232
                {
 
233
                    x -= r.width() - offset.x();
 
234
                }
 
235
 
 
236
                // try to keep this on the screen
 
237
                if (x - popup->width() < 0)
 
238
                {
 
239
                    x = r.left();
 
240
                }
 
241
 
 
242
                return QPoint(x, y);
 
243
            }
 
244
            else
 
245
            {
 
246
                QDesktopWidget* desktop = QApplication::desktop();
 
247
                QRect screen = desktop->screenGeometry(desktop->screenNumber(const_cast<QWidget*>(source)));
 
248
                x = r.left() + offset.x();
 
249
 
 
250
                // try to keep this on the screen
 
251
                if (x + popup->width() > screen.right())
 
252
                {
 
253
                    x = r.right() - popup->width() + 1 + offset.x();
 
254
 
 
255
                    if (x < screen.left())
 
256
                    {
 
257
                        x = screen.left();
 
258
                    }
 
259
                }
 
260
            }
 
261
 
 
262
            return QPoint(x, y);
 
263
        }
 
264
    }
 
265
}
 
266
 
 
267
void colorize(QImage& image)
 
268
{
 
269
    KConfigGroup config(KGlobal::config(), "WM");
 
270
    QPalette pal = QApplication::palette();
 
271
    pal.setCurrentColorGroup( QPalette::Active );
 
272
    QColor color = pal.color( QPalette::Highlight );
 
273
    QColor activeTitle = config.readEntry("activeBackground", color);
 
274
    QColor inactiveTitle = config.readEntry("inactiveBackground", color);
 
275
 
 
276
    // figure out which color is most suitable for recoloring to
 
277
    int h1, s1, v1, h2, s2, v2, h3, s3, v3;
 
278
    activeTitle.getHsv(&h1, &s1, &v1);
 
279
    inactiveTitle.getHsv(&h2, &s2, &v2);
 
280
    QApplication::palette().color(QPalette::Active, QPalette::Window).getHsv(&h3, &s3, &v3);
 
281
 
 
282
    if ( (qAbs(h1-h3)+qAbs(s1-s3)+qAbs(v1-v3) < qAbs(h2-h3)+qAbs(s2-s3)+qAbs(v2-v3)) &&
 
283
            ((qAbs(h1-h3)+qAbs(s1-s3)+qAbs(v1-v3) < 32) || (s1 < 32)) && (s2 > s1))
 
284
        color = inactiveTitle;
 
285
    else
 
286
        color = activeTitle;
 
287
 
 
288
    // limit max/min brightness
 
289
    int r, g, b;
 
290
    color.getRgb(&r, &g, &b);
 
291
    int gray = qGray(r, g, b);
 
292
    if (gray > 180) {
 
293
        r = (r - (gray - 180) < 0 ? 0 : r - (gray - 180));
 
294
        g = (g - (gray - 180) < 0 ? 0 : g - (gray - 180));
 
295
        b = (b - (gray - 180) < 0 ? 0 : b - (gray - 180));
 
296
    } else if (gray < 76) {
 
297
        r = (r + (76 - gray) > 255 ? 255 : r + (76 - gray));
 
298
        g = (g + (76 - gray) > 255 ? 255 : g + (76 - gray));
 
299
        b = (b + (76 - gray) > 255 ? 255 : b + (76 - gray));
 
300
    }
 
301
    color.setRgb(r, g, b);
 
302
    KIconEffect::colorize(image, color, 1.0);
 
303
}
 
304
 
 
305
QColor blendColors(const QColor& c1, const QColor& c2)
 
306
{
 
307
    int r1, g1, b1;
 
308
    int r2, g2, b2;
 
309
 
 
310
    c1.getRgb(&r1, &g1, &b1);
 
311
    c2.getRgb(&r2, &g2, &b2);
 
312
 
 
313
    r1 += (int) (.5 * (r2 - r1));
 
314
    g1 += (int) (.5 * (g2 - g1));
 
315
    b1 += (int) (.5 * (b2 - b1));
 
316
 
 
317
    return QColor(r1, g1, b1);
 
318
}
 
319
 
 
320
QColor shadowColor(const QColor& c)
 
321
{
 
322
    int r = c.red();
 
323
    int g = c.green();
 
324
    int b = c.blue();
 
325
 
 
326
    if ( r < 128 )
 
327
        r = 255;
 
328
    else
 
329
        r = 0;
 
330
 
 
331
    if ( g < 128 )
 
332
        g = 255;
 
333
    else
 
334
        g = 0;
 
335
 
 
336
    if ( b < 128 )
 
337
        b = 255;
 
338
    else
 
339
        b = 0;
 
340
 
 
341
    return QColor( r, g, b );
 
342
}
 
343
 
 
344
QIcon menuIconSet(const QString& icon)
 
345
{
 
346
    QIcon iconset;
 
347
    int iconSize = KickerSettings::menuEntryHeight();
 
348
    if (iconSize > 0)
 
349
    {
 
350
        iconset = KIconLoader::global()->loadIconSet(icon,
 
351
                                                     K3Icon::NoGroup,
 
352
                                                     iconSize);
 
353
    }
 
354
    else if (iconSize == 0)
 
355
    {
 
356
        QPixmap normal = KIconLoader::global()->loadIcon(icon,
 
357
                                                         K3Icon::Small,
 
358
                                                         0,
 
359
                                                         K3Icon::DefaultState,
 
360
                                                         QStringList(), 0,
 
361
                                                         true);
 
362
 
 
363
        QPixmap active = KIconLoader::global()->loadIcon(icon,
 
364
                                                         K3Icon::Small,
 
365
                                                         0,
 
366
                                                         K3Icon::ActiveState,
 
367
                                                         QStringList(), 0,
 
368
                                                         true);
 
369
        // make sure they are not larger than 20x20
 
370
        if (normal.width() > 20 || normal.height() > 20)
 
371
        {
 
372
            normal = normal.scaled(20,20, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
 
373
        }
 
374
 
 
375
        if (active.width() > 20 || active.height() > 20)
 
376
        {
 
377
            active = active.scaled(20,20, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
 
378
        }
 
379
 
 
380
        iconset.addPixmap(normal, QIcon::Normal, QIcon::On);
 
381
        iconset.addPixmap(active, QIcon::Active, QIcon::Off);
 
382
    }
 
383
 
 
384
    return iconset;
 
385
}
 
386
 
 
387
} // namespace
 
388