1
by Lior Kaplan
Import upstream version 1.4.3 |
1 |
/***************************************************************************
|
2 |
kbpickicondlg.cpp - description
|
|
3 |
-------------------
|
|
4 |
begin : Sat Jul 21 2001
|
|
5 |
copyright : (C) 2001 by Leonid Zeitlin
|
|
6 |
email : lz@europe.com
|
|
7 |
***************************************************************************/
|
|
8 |
||
9 |
/***************************************************************************
|
|
10 |
* *
|
|
11 |
* This program is free software; you can redistribute it and/or modify *
|
|
12 |
* it under the terms of the GNU General Public License as published by *
|
|
13 |
* the Free Software Foundation; either version 2 of the License, or *
|
|
14 |
* (at your option) any later version. *
|
|
15 |
* *
|
|
16 |
***************************************************************************/
|
|
17 |
||
18 |
#include "kbpickicondlg.h" |
|
19 |
||
20 |
#include <qvbox.h> |
|
21 |
#include <qdir.h> |
|
22 |
#include <qwhatsthis.h> |
|
23 |
#include <qpushbutton.h> |
|
24 |
#include <qimage.h> |
|
25 |
||
26 |
#include <kdeversion.h> |
|
27 |
#include <klocale.h> |
|
28 |
#if KDE_VERSION_MAJOR >= 3
|
|
29 |
#include <kstandarddirs.h> |
|
30 |
#else
|
|
31 |
#include <kstddirs.h> |
|
32 |
#endif
|
|
33 |
#include <kglobal.h> |
|
34 |
#include <kfiledialog.h> |
|
35 |
#include <kmessagebox.h> |
|
36 |
||
37 |
#include "kbconfig.h" |
|
38 |
#include "pathlistboxitem.h" |
|
39 |
||
40 |
KBPickIconDlg::KBPickIconDlg(const QString ¤tPath, const QPixmap ¤tPixmap, |
|
41 |
QWidget *parent, const char *name ) |
|
42 |
: KDialogBase(parent, name, true /*modal*/, i18n("Pick an icon") /*caption*/, Ok | Cancel) { |
|
43 |
QVBox *page = makeVBoxMainWidget(); |
|
44 |
||
45 |
lbIcons = new KListBox(page); |
|
46 |
QObject::connect(lbIcons, SIGNAL(doubleClicked(QListBoxItem*)), |
|
47 |
this, SLOT(slotOk())); |
|
48 |
QObject::connect(lbIcons, SIGNAL(returnPressed(QListBoxItem*)), |
|
49 |
this, SLOT(slotOk())); |
|
50 |
QWhatsThis::add(lbIcons, i18n("Select one of the icons")); |
|
51 |
||
52 |
QPushButton *btnBrowse = new QPushButton(i18n("&Browse..."), page); |
|
53 |
QObject::connect(btnBrowse, SIGNAL(clicked()), this, SLOT(slotBrowseForIcon())); |
|
54 |
QWhatsThis::add(btnBrowse, i18n("Browse for an image file to use as an icon")); |
|
55 |
||
56 |
loadCountryFlags(); |
|
57 |
||
58 |
// I am told in Red Hat 9 standard KDE flag pixmaps are missing.
|
|
59 |
// Workaround: we have to simulate them by rescaling GKB's pixmaps
|
|
60 |
if (lbIcons->count() == 0) { |
|
61 |
loadGkbCountryFlags(); |
|
62 |
}
|
|
63 |
lbIcons->sort(); |
|
64 |
showCurrentPath(currentPath, currentPixmap); |
|
65 |
lbIcons->setFocus(); |
|
66 |
}
|
|
67 |
||
68 |
KBPickIconDlg::~KBPickIconDlg(){ |
|
69 |
}
|
|
70 |
||
71 |
/** Get the path name of the selected icon. Returns empty string if no icon selected */
|
|
72 |
QString KBPickIconDlg::getIconPath(){ |
|
73 |
QListBoxItem *item = lbIcons->selectedItem(); |
|
74 |
if (item) |
|
75 |
return dynamic_cast<PathListBoxItem*>(item)->path; |
|
76 |
else return QString::null; // should not happen |
|
77 |
}
|
|
78 |
||
79 |
/** No descriptions */
|
|
80 |
const QPixmap* KBPickIconDlg::getIcon(){ |
|
81 |
if (lbIcons->currentItem() != -1) |
|
82 |
return lbIcons->pixmap(lbIcons->currentItem()); |
|
83 |
else return NULL; |
|
84 |
}
|
|
85 |
||
86 |
/** Browse for an arbitrary icon file */
|
|
87 |
void KBPickIconDlg::slotBrowseForIcon() |
|
88 |
{
|
|
89 |
QString iconPath = KFileDialog::getOpenFileName(QString::null, |
|
90 |
i18n("*.png *.jpg *.gif *.xpm|Icon files (*.png, *.jpg, *.gif, *.xpm)\n*.*|All files (*.*)")); |
|
91 |
if (iconPath.isEmpty()) return; |
|
92 |
QImage img; |
|
93 |
if (img.load(iconPath)) { |
|
94 |
double aspectRatio = img.width() / img.height(); |
|
95 |
QString message = QString::null; |
|
96 |
bool too_big, too_wide, too_narrow; |
|
97 |
too_narrow = aspectRatio < FLAG_ICON_WIDTH / FLAG_ICON_HEIGHT - 0.1; |
|
98 |
too_wide = aspectRatio > FLAG_ICON_WIDTH / FLAG_ICON_HEIGHT + 0.1; |
|
99 |
too_big = img.width() > FLAG_ICON_WIDTH * 2; |
|
100 |
if (too_big || too_narrow || too_wide) { |
|
101 |
message = i18n("The size of this image (%1 by %2) is not good.\n" |
|
102 |
"Preferred size for the layout icons is %3 by %4.\n") |
|
103 |
.arg(img.width()).arg(img.height()).arg(FLAG_ICON_WIDTH).arg(FLAG_ICON_HEIGHT); |
|
104 |
if (too_big) { |
|
105 |
QString msg_tail = ""; |
|
106 |
if (too_wide) msg_tail = i18n(" and also too wide"); |
|
107 |
else if (too_narrow) msg_tail += i18n(" and also too narrow"); |
|
108 |
message += i18n("This image is too big%1.").arg(msg_tail); |
|
109 |
}
|
|
110 |
else if (too_wide) message += i18n("This image is too wide."); |
|
111 |
else if (too_narrow) message += i18n("This image is too narrow."); |
|
112 |
message += "\n"; |
|
113 |
message += i18n("KKBSwitch will scale it to appropriate size, but the result may not look very good.\n" |
|
114 |
"Are you sure you want to use this image?"); |
|
115 |
if (KMessageBox::questionYesNo(this, message) != KMessageBox::Yes) return; |
|
116 |
}
|
|
117 |
if (img.width() > FLAG_ICON_WIDTH + 3 || img.height() > FLAG_ICON_HEIGHT + 3) |
|
118 |
img = img.smoothScale(FLAG_ICON_WIDTH, FLAG_ICON_HEIGHT); |
|
119 |
QPixmap pix; |
|
120 |
pix.convertFromImage(img); |
|
121 |
PathListBoxItem *item = new PathListBoxItem(lbIcons, pix, QFileInfo(iconPath).fileName(), |
|
122 |
iconPath); |
|
123 |
lbIcons->setSelected(item, true); |
|
124 |
lbIcons->centerCurrentItem(); |
|
125 |
}
|
|
126 |
else KMessageBox::error(this, i18n("Cannot read icon from file %1. " |
|
127 |
"Either it is not an image file or it is corrupt.").arg(iconPath)); |
|
128 |
}
|
|
129 |
||
130 |
void KBPickIconDlg::loadCountryFlags() |
|
131 |
{
|
|
132 |
QPixmap pix; |
|
133 |
QDir dir; |
|
134 |
QStringList locales; |
|
135 |
QString path; |
|
136 |
QStringList dirs = KGlobal::dirs()->findDirs("locale", "l10n"); |
|
137 |
||
138 |
for (QStringList::Iterator dirIter = dirs.begin(); dirIter != dirs.end(); dirIter++) { |
|
139 |
dir.setPath(*dirIter); |
|
140 |
locales = dir.entryList(QDir::Dirs, QDir::Name); |
|
141 |
for (QStringList::Iterator iter = locales.begin(); iter != locales.end(); iter++) { |
|
142 |
path = dir.path() + "/" + *iter + "/flag.png"; |
|
143 |
if (*iter != "." && *iter != ".." && pix.load(path)) { |
|
144 |
KConfig config(dir.path() + "/" + *iter + "/entry.desktop", true, false, |
|
145 |
"locale" /*doesn't really matter*/); |
|
146 |
config.setGroup("KCM Locale"); |
|
147 |
new PathListBoxItem(lbIcons, pix, config.readEntry("Name"), path); |
|
148 |
}
|
|
149 |
}
|
|
150 |
}
|
|
151 |
}
|
|
152 |
||
153 |
void KBPickIconDlg::loadGkbCountryFlags() |
|
154 |
{
|
|
155 |
QDir dir; |
|
156 |
QString path, code, name; |
|
157 |
QPixmap pix; |
|
158 |
QImage img; |
|
159 |
||
160 |
dir.setPath("/usr/share/pixmaps/gkb"); |
|
161 |
const QFileInfoList *icons = dir.entryInfoList(QDir::Files, QDir::Name); |
|
162 |
if (icons) { |
|
163 |
QFileInfoListIterator iter(*icons); |
|
164 |
QFileInfo *info; |
|
165 |
for (; (info = iter.current()); ++iter) { |
|
166 |
path = info->filePath(); |
|
167 |
code = info->baseName(); |
|
168 |
if (img.load(path)) { |
|
169 |
KConfig config("l10n/" + code + "/entry.desktop", true, false, "locale"); |
|
170 |
config.setGroup("KCM Locale"); |
|
171 |
name = config.readEntry("Name"); |
|
172 |
if (!name.isNull()) { |
|
173 |
pix.convertFromImage(img.smoothScale(FLAG_ICON_WIDTH, FLAG_ICON_HEIGHT)); |
|
174 |
new PathListBoxItem(lbIcons, pix, name, path); |
|
175 |
}
|
|
176 |
}
|
|
177 |
}
|
|
178 |
}
|
|
179 |
}
|
|
180 |
||
181 |
void KBPickIconDlg::showCurrentPath(const QString ¤tPath, |
|
182 |
const QPixmap ¤tPixmap) |
|
183 |
{
|
|
184 |
PathListBoxItem *item = NULL; |
|
185 |
bool itemFound = false; |
|
186 |
for (unsigned i = 0; i < lbIcons->count(); i++) { |
|
187 |
item = dynamic_cast<PathListBoxItem*>(lbIcons->item(i)); |
|
188 |
if (item->path == currentPath) { |
|
189 |
itemFound = true; |
|
190 |
break; |
|
191 |
}
|
|
192 |
}
|
|
193 |
// why this strange manipulation of HScrollBarMode?
|
|
194 |
// Strangely, without it, if the selected item is the last in the listbox, it ends up
|
|
195 |
// being obscured by the horizontal scrollbar
|
|
196 |
lbIcons->setHScrollBarMode(QScrollView::AlwaysOn); |
|
197 |
if (!itemFound) item = new PathListBoxItem(lbIcons, currentPixmap, |
|
198 |
QFileInfo(currentPath).fileName(), currentPath); |
|
199 |
lbIcons->updateScrollBars(); |
|
200 |
lbIcons->setSelected(item, true); |
|
201 |
lbIcons->ensureCurrentVisible(); |
|
202 |
lbIcons->setHScrollBarMode(QScrollView::Auto); |
|
203 |
}
|