8a50e160341f8abe393580f1b490b8c5fe50cd44
[case] / src / utils.cpp
1 // case - file manager for N900
2 // Copyright (C) 2010 Lukas Hrazky <lukkash@email.cz>
3 // 
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 // 
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 // 
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17
18 #include "utils.h"
19
20 #include <QFSFileEngine>
21
22
23 QString shortenPath(const QString &path) {
24     QString homePath = QFSFileEngine::homePath();
25
26     if (path.indexOf(homePath, 0) == 0) {
27         QString result = path;
28
29         result.replace(0, homePath.size(), "~");
30         return result;
31     }
32
33     return path;
34 }
35
36
37 QString unwindPath(const QString &path) {
38     QString result = path;
39     // if ~ is the first character and / or nothing follows it, replace with home dir
40     if (path == "~" || path.indexOf("~/", 0) == 0) {
41         QString homePath = QFSFileEngine::homePath();
42         result.replace(0, 1, homePath);
43     // in case someone wants to enter a dir called ~ in the current dir, he can escape it with \~
44     } else if (path == "\\~" || path.indexOf("\\~/", 0) == 0) {
45         result.replace(0, 2, "~");
46     }
47
48     return result;
49 }
50
51
52 void themeImage(const QPalette &p, QImage &image, const bool inverse) {
53     image = image.convertToFormat(QImage::Format_Indexed8);
54
55     QRgb highlight = p.highlight().color().rgb();
56     QRgb second;
57     if (inverse) {
58         second = p.text().color().rgb();
59     } else {
60         second = p.buttonText().color().rgb();
61     }
62
63     QVector<QRgb> colorTable = image.colorTable();
64     for (QVector<QRgb>::iterator it = colorTable.begin(); it != colorTable.end(); ++it) {
65         if ((*it & 0xFFFFFF) == 0xFFFFFF) *it = (*it & 0xFF000000) | second;
66         else if ((*it & 0xFFFFFF) == 0x0000FF) *it = (*it & 0xFF000000) | highlight;
67     }
68     image.setColorTable(colorTable);
69 }
70
71
72 bool loadOperationIcons(const QPalette &p, const QString &name, QPixmap &normal, QPixmap &inverse) {
73     QImage iconImage(ICON_PATH + ICON_SET + "/" + name + ".xpm", "XPM");
74     if (iconImage.isNull()) iconImage.load(ICON_PATH + ICON_SET + "/" + name + ".png", "PNG");
75     if (iconImage.isNull()) iconImage.load(ICON_PATH + ICON_SET + "/" + name + ".gif", "GIF");
76
77     QImage inverseIconImage = iconImage;
78
79     themeImage(p, iconImage, false);
80     themeImage(p, inverseIconImage, true);
81
82     normal = QPixmap::fromImage(iconImage);
83     inverse = QPixmap::fromImage(inverseIconImage);
84     return true;
85 }
86
87
88 bool loadMiddleButtonIcons(const QPalette &p, const QString &name, QIcon &normal, QIcon &mirrored) {
89     QImage iconImage(ICON_PATH + ICON_SET + "/" + name + ".xpm", "XPM");
90     if (iconImage.isNull()) iconImage.load(ICON_PATH + ICON_SET + "/" + name + ".png", "PNG");
91     if (iconImage.isNull()) iconImage.load(ICON_PATH + ICON_SET + "/" + name + ".gif", "GIF");
92
93     themeImage(p, iconImage, false);
94
95     normal = QIcon(QPixmap::fromImage(iconImage));
96     mirrored = QIcon(QPixmap::fromImage(iconImage.mirrored(true, false)));
97     return true;
98 }