1
/***************************************************************************
2
kbpickicondlg.cpp - description
4
begin : Sat Jul 21 2001
5
copyright : (C) 2001 by Leonid Zeitlin
7
***************************************************************************/
9
/***************************************************************************
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. *
16
***************************************************************************/
18
#include "kbpickicondlg.h"
22
#include <qwhatsthis.h>
23
#include <qpushbutton.h>
26
#include <kdeversion.h>
28
#if KDE_VERSION_MAJOR >= 3
29
#include <kstandarddirs.h>
34
#include <kfiledialog.h>
35
#include <kmessagebox.h>
38
#include "pathlistboxitem.h"
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();
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"));
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"));
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();
64
showCurrentPath(currentPath, currentPixmap);
68
KBPickIconDlg::~KBPickIconDlg(){
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();
75
return dynamic_cast<PathListBoxItem*>(item)->path;
76
else return QString::null; // should not happen
79
/** No descriptions */
80
const QPixmap* KBPickIconDlg::getIcon(){
81
if (lbIcons->currentItem() != -1)
82
return lbIcons->pixmap(lbIcons->currentItem());
86
/** Browse for an arbitrary icon file */
87
void KBPickIconDlg::slotBrowseForIcon()
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;
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);
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);
110
else if (too_wide) message += i18n("This image is too wide.");
111
else if (too_narrow) message += i18n("This image is too narrow.");
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;
117
if (img.width() > FLAG_ICON_WIDTH + 3 || img.height() > FLAG_ICON_HEIGHT + 3)
118
img = img.smoothScale(FLAG_ICON_WIDTH, FLAG_ICON_HEIGHT);
120
pix.convertFromImage(img);
121
PathListBoxItem *item = new PathListBoxItem(lbIcons, pix, QFileInfo(iconPath).fileName(),
123
lbIcons->setSelected(item, true);
124
lbIcons->centerCurrentItem();
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));
130
void KBPickIconDlg::loadCountryFlags()
136
QStringList dirs = KGlobal::dirs()->findDirs("locale", "l10n");
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);
153
void KBPickIconDlg::loadGkbCountryFlags()
156
QString path, code, name;
160
dir.setPath("/usr/share/pixmaps/gkb");
161
const QFileInfoList *icons = dir.entryInfoList(QDir::Files, QDir::Name);
163
QFileInfoListIterator iter(*icons);
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);
181
void KBPickIconDlg::showCurrentPath(const QString ¤tPath,
182
const QPixmap ¤tPixmap)
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) {
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);