d883c844ca6bdcb6c6a67cd29abab98c6e1fe1b6
[case] / src / button.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 "button.h"
19
20 #include <iostream>
21
22 #define ICON_PATH QString("/usr/share/pixmaps/case/")
23 #define ICON_SET "default"
24
25
26 Button::Button(const QString &name, QWidget *parent, const int maxWidth, const int maxHeight) :
27     QPushButton(parent),
28     iconIndex(0)
29 {
30     setMaximumWidth(maxWidth);
31     setMaximumHeight(maxHeight);
32
33     QImage iconImage(ICON_PATH + ICON_SET + "/" + name + ".xpm", "XPM");
34     if (iconImage.isNull()) iconImage.load(ICON_PATH + ICON_SET + "/" + name + ".png", "PNG");
35     if (iconImage.isNull()) iconImage.load(ICON_PATH + ICON_SET + "/" + name + ".gif", "GIF");
36
37     iconImage = iconImage.convertToFormat(QImage::Format_Indexed8);
38
39     QRgb buttonText = palette().color(QPalette::ButtonText).rgb();
40     QRgb highlight = palette().color(QPalette::Highlight).rgb();
41     QVector<QRgb> colorTable = iconImage.colorTable();
42     for (QVector<QRgb>::iterator it = colorTable.begin(); it != colorTable.end(); ++it) {
43         if ((*it & 0xFFFFFF) == 0xFFFFFF) *it = (*it & 0xFF000000) | buttonText;
44         else if ((*it & 0xFFFFFF) == 0x0000FF) *it = (*it & 0xFF000000) | highlight;
45     }
46     iconImage.setColorTable(colorTable);
47
48     icons[0] = QIcon(QPixmap::fromImage(iconImage));
49     icons[1] = QIcon(QPixmap::fromImage(iconImage.mirrored(true, false)));
50
51     setIcon(icons[0]);
52 }
53
54
55 void Button::swapIcon() {
56     iconIndex = (iconIndex + 1) % 2;
57     setIcon(icons[iconIndex]);
58 }