KKBSwitch – keyboard layout indicator
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

kbpickicondlg.cpp 7.5KB

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