summaryrefslogtreecommitdiffstats
path: root/kkbswitch/kbpickicondlg.cpp
blob: b3821ef58ee2f376f1ba06d7a1d75b78b004f4c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/***************************************************************************
                          kbpickicondlg.cpp  -  description
                             -------------------
    begin                : Sat Jul 21 2001
    copyright            : (C) 2001 by Leonid Zeitlin
    email                : lz@europe.com
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "kbpickicondlg.h"

#include <ntqvbox.h>
#include <ntqdir.h>
#include <ntqwhatsthis.h>
#include <ntqpushbutton.h>
#include <ntqimage.h>

#include <tdeversion.h>
#include <tdelocale.h>
#if TDE_VERSION_MAJOR >= 3
  #include <kstandarddirs.h>
#else
  #include <kstddirs.h>
#endif
#include <tdeglobal.h>
#include <tdefiledialog.h>
#include <tdemessagebox.h>

#include "kbconfig.h"
#include "pathlistboxitem.h"

KBPickIconDlg::KBPickIconDlg(const TQString &currentPath, const TQPixmap &currentPixmap,
  TQWidget *parent, const char *name )
  : KDialogBase(parent, name, true /*modal*/, i18n("Pick an icon") /*caption*/, Ok | Cancel) {
  TQVBox *page = makeVBoxMainWidget();
  
  lbIcons = new TDEListBox(page);
  TQObject::connect(lbIcons, SIGNAL(doubleClicked(TQListBoxItem*)),
    this, SLOT(slotOk()));
  TQObject::connect(lbIcons, SIGNAL(returnPressed(TQListBoxItem*)),
    this, SLOT(slotOk()));
  TQWhatsThis::add(lbIcons, i18n("Select one of the icons"));  

  TQPushButton *btnBrowse = new TQPushButton(i18n("&Browse..."), page);
  TQObject::connect(btnBrowse, SIGNAL(clicked()), this, SLOT(slotBrowseForIcon()));
  TQWhatsThis::add(btnBrowse, i18n("Browse for an image file to use as an icon"));
  
  loadCountryFlags();
      
	// I am told in Red Hat 9 standard KDE flag pixmaps are missing.
  // Workaround: we have to simulate them by rescaling GKB's pixmaps
  if (lbIcons->count() == 0) {
		loadGkbCountryFlags();
	}
	lbIcons->sort();
	showCurrentPath(currentPath, currentPixmap);
  lbIcons->setFocus();
}

KBPickIconDlg::~KBPickIconDlg(){
}

/** Get the path name of the selected icon. Returns empty string if no icon selected */
TQString KBPickIconDlg::getIconPath(){
  TQListBoxItem *item = lbIcons->selectedItem();
	if (item) 
		return dynamic_cast<PathListBoxItem*>(item)->path;
	else return TQString::null; // should not happen
}

/** No descriptions */
const TQPixmap* KBPickIconDlg::getIcon(){
  if (lbIcons->currentItem() != -1)
    return lbIcons->pixmap(lbIcons->currentItem());
  else return NULL;
}

/** Browse for an arbitrary icon file */
void KBPickIconDlg::slotBrowseForIcon()
{
	TQString iconPath = KFileDialog::getOpenFileName(TQString::null,
	  i18n("*.png *.jpg *.gif *.xpm|Icon files (*.png, *.jpg, *.gif, *.xpm)\n*.*|All files (*.*)"));
	if (iconPath.isEmpty()) return;  
  TQImage img;
  if (img.load(iconPath)) {
		double aspectRatio = img.width() / img.height();
		TQString message = TQString::null;
		bool too_big, too_wide, too_narrow;
		too_narrow = aspectRatio < FLAG_ICON_WIDTH / FLAG_ICON_HEIGHT - 0.1;
		too_wide = aspectRatio > FLAG_ICON_WIDTH / FLAG_ICON_HEIGHT + 0.1;
		too_big = img.width() > FLAG_ICON_WIDTH * 2;
		if (too_big || too_narrow || too_wide) {
  		message = i18n("The size of this image (%1 by %2) is not good.\n"
	  	  "Preferred size for the layout icons is %3 by %4.\n")
		    .arg(img.width()).arg(img.height()).arg(FLAG_ICON_WIDTH).arg(FLAG_ICON_HEIGHT);
      if (too_big) {
        TQString msg_tail = "";
        if (too_wide) msg_tail = i18n(" and also too wide");
				else if (too_narrow) msg_tail += i18n(" and also too narrow");
				message += i18n("This image is too big%1.").arg(msg_tail);
			}
			else if (too_wide) message += i18n("This image is too wide.");
			else if (too_narrow) message += i18n("This image is too narrow.");
  		message += "\n";
      message += i18n("KKBSwitch will scale it to appropriate size, but the result may not look very good.\n"
        "Are you sure you want to use this image?");
      if (KMessageBox::questionYesNo(this, message)	!= KMessageBox::Yes) return;
		}    	  
		if (img.width() > FLAG_ICON_WIDTH + 3 || img.height() > FLAG_ICON_HEIGHT + 3)
		  img = img.smoothScale(FLAG_ICON_WIDTH, FLAG_ICON_HEIGHT);
		TQPixmap pix;
		pix.convertFromImage(img);
    PathListBoxItem *item = new PathListBoxItem(lbIcons, pix, TQFileInfo(iconPath).fileName(), 
      iconPath);
    lbIcons->setSelected(item, true);
    lbIcons->centerCurrentItem();
  }
  else KMessageBox::error(this, i18n("Cannot read icon from file %1. "
    "Either it is not an image file or it is corrupt.").arg(iconPath));
}

void KBPickIconDlg::loadCountryFlags()
{
  TQPixmap pix;
  TQDir dir;
  TQStringList locales;
  TQString path;
  TQStringList dirs = TDEGlobal::dirs()->findDirs("locale", "l10n");
  
  for (TQStringList::Iterator dirIter = dirs.begin(); dirIter != dirs.end(); dirIter++) {
    dir.setPath(*dirIter);
    locales = dir.entryList(TQDir::Dirs, TQDir::Name);
    for (TQStringList::Iterator iter = locales.begin(); iter != locales.end(); iter++) {
      path = dir.path() + "/" + *iter + "/flag.png";
      if (*iter != "." && *iter != ".." && pix.load(path)) {
				TDEConfig config(dir.path() + "/" + *iter + "/entry.desktop", true, false,
				  "locale" /*doesn't really matter*/);
        config.setGroup("KCM Locale");
        new PathListBoxItem(lbIcons, pix, config.readEntry("Name"), path);
      }
    }
  }
}
	
void KBPickIconDlg::loadGkbCountryFlags()
{
  TQDir dir;
  TQString path, code, name;
  TQPixmap pix;
  TQImage img;
	
  dir.setPath("/usr/share/pixmaps/gkb");
  const TQFileInfoList *icons = dir.entryInfoList(TQDir::Files, TQDir::Name);
  if (icons) {
		TQFileInfoListIterator iter(*icons);
		TQFileInfo *info;
    for (; (info = iter.current()); ++iter) {
      path = info->filePath();
      code = info->baseName();
      if (img.load(path)) {
				TDEConfig config("l10n/" + code + "/entry.desktop", true, false, "locale");
				config.setGroup("KCM Locale");
				name = config.readEntry("Name");
				if (!name.isNull()) {
					pix.convertFromImage(img.smoothScale(FLAG_ICON_WIDTH, FLAG_ICON_HEIGHT));
          new PathListBoxItem(lbIcons, pix, name, path);
        }
      }
    }
  }
}

void KBPickIconDlg::showCurrentPath(const TQString &currentPath,
  const TQPixmap &currentPixmap)
{
	PathListBoxItem *item = NULL;
  bool itemFound = false;
  for (unsigned i = 0; i < lbIcons->count(); i++) {
		item = dynamic_cast<PathListBoxItem*>(lbIcons->item(i));
		if (item->path == currentPath) {
      itemFound = true;
      break;
    }  
	}
  // why this strange manipulation of HScrollBarMode?
  // Strangely, without it, if the selected item is the last in the listbox, it ends up
  // being obscured by the horizontal scrollbar
  lbIcons->setHScrollBarMode(TQScrollView::AlwaysOn);
  if (!itemFound) item = new PathListBoxItem(lbIcons, currentPixmap, 
    TQFileInfo(currentPath).fileName(), currentPath);
  lbIcons->updateScrollBars();  
  lbIcons->setSelected(item, true);
	lbIcons->ensureCurrentVisible();
  lbIcons->setHScrollBarMode(TQScrollView::Auto);  
}