kexi

pixmapcollection.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2004 Cedric Pasteur <cedric.pasteur@free.fr>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include <qpixmap.h>
00021 #include <qlayout.h>
00022 #include <qlabel.h>
00023 #include <qstringlist.h>
00024 #include <qtoolbutton.h>
00025 #include <qdom.h>
00026 
00027 #include <kapplication.h>
00028 #include <kiconloader.h>
00029 #include <kfiledialog.h>
00030 #include <kcombobox.h>
00031 #include <kicondialog.h>
00032 #include <klineedit.h>
00033 #include <kicontheme.h>
00034 #include <kpixmapio.h>
00035 #include <kpopupmenu.h>
00036 #include <kdebug.h>
00037 #include <klocale.h>
00038 #include <kmessagebox.h>
00039 
00040 #include "pixmapcollection.h"
00041 
00043 PixmapCollection::PixmapCollection(const QString &collectionName, QObject *parent, const char *name)
00044  : QObject(parent, name)
00045 {
00046     m_name = collectionName;
00047 }
00048 
00049 QString
00050 PixmapCollection::addPixmapPath(const KURL &url)
00051 {
00052     QString name = url.filename();
00053     while(m_pixmaps.contains(name))
00054     {
00055         bool ok;
00056         int num = name.right(1).toInt(&ok, 10);
00057         if(ok)
00058             name = name.left(name.length()-1) + QString::number(num+1);
00059         else
00060             name += "2";
00061     }
00062 
00063     m_pixmaps.insert(name, qMakePair(url.path(), 0));
00064     return name;
00065 }
00066 
00067 QString
00068 PixmapCollection::addPixmapName(const QString &icon, int size)
00069 {
00070     QString name = icon;
00071     while(m_pixmaps.contains(name))
00072     {
00073         bool ok;
00074         int num = name.right(1).toInt(&ok, 10);
00075         if(ok)
00076             name = name.left(name.length()-1) + QString::number(num+1);
00077         else
00078             name += "2";
00079     }
00080 
00081     m_pixmaps.insert(name, qMakePair(icon, size));
00082     return name;
00083 }
00084 
00085 void
00086 PixmapCollection::removePixmap(const QString &name)
00087 {
00088     m_pixmaps.remove(name);
00089 }
00090 
00091 QPixmap
00092 PixmapCollection::getPixmap(const QString &name)
00093 {
00094     if(!m_pixmaps.contains(name))
00095     {
00096         kdDebug() << " The icon " << name << " you requested is not in the collection" << endl;
00097         return QPixmap();
00098     }
00099 
00100     if(m_pixmaps[name].second != 0)
00101     {
00102         return kapp->iconLoader()->loadIcon(m_pixmaps[name].first, KIcon::NoGroup, m_pixmaps[name].second);
00103     }
00104     else
00105         return QPixmap(m_pixmaps[name].first);
00106 }
00107 
00108 bool
00109 PixmapCollection::contains(const QString &name)
00110 {
00111     return m_pixmaps.contains(name);
00112 }
00113 
00114 void
00115 PixmapCollection::save(QDomNode parentNode)
00116 {
00117     if(m_pixmaps.isEmpty())
00118         return;
00119 
00120     QDomDocument domDoc = parentNode.ownerDocument();
00121     QDomElement collection = domDoc.createElement("collection");
00122     parentNode.appendChild(collection);
00123 
00124     PixmapMap::ConstIterator it;
00125     PixmapMap::ConstIterator endIt = m_pixmaps.constEnd();
00126     for(it = m_pixmaps.constBegin(); it != endIt; ++it)
00127     {
00128         QDomElement item = domDoc.createElement("pixmap");
00129         collection.appendChild(item);
00130         item.setAttribute("name", it.key());
00131         if(it.data().second != 0)
00132             item.setAttribute("size", QString::number(it.data().second));
00133 
00134         QString text = it.data().first;
00135         QDomText textNode = domDoc.createTextNode(text);
00136         item.appendChild(textNode);
00137     }
00138 }
00139 
00140 void
00141 PixmapCollection::load(QDomNode node)
00142 {
00143     QDomDocument domDoc = node.ownerDocument();
00144     for(QDomNode n = node.firstChild(); !n.isNull(); n = n.nextSibling())
00145     {
00146         QDomElement el = n.toElement();
00147         QPair<QString, int> pair = qMakePair(el.text(), el.attribute("size").toInt());
00148         m_pixmaps[el.attribute("name")] = pair;
00149     }
00150 }
00151 
00153 LoadIconDialog::LoadIconDialog(QWidget *parent)
00154 : KDialogBase(parent, "loadicon_dialog", true, i18n("Load KDE Icon by Name"), Ok|Cancel, Ok, false)
00155 {
00156     QFrame *frame = makeMainWidget();
00157     QGridLayout *l = new QGridLayout(frame, 2, 3, 0, 6);
00158 
00159     // Name input
00160     QLabel *name = new QLabel(i18n("&Name:"), frame);
00161     l->addWidget(name, 0, 0);
00162     name->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
00163     m_nameInput = new KLineEdit("kexi", frame);
00164     l->addWidget(m_nameInput, 0, 1);
00165     name->setBuddy(m_nameInput);
00166 
00167     // Choose size
00168     QLabel *size = new QLabel(i18n("&Size:"), frame);
00169     l->addWidget(size, 1, 0);
00170     size->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
00171 
00172     KComboBox *combo = new KComboBox(frame);
00173     l->addWidget(combo, 1, 1);
00174     size->setBuddy(combo);
00175     QStringList list;
00176     list << i18n("Small") << i18n("Medium") << i18n("Large") << i18n("Huge");
00177     combo->insertStringList(list);
00178     combo->setCurrentItem(2);
00179     connect(combo, SIGNAL(activated(int)), this, SLOT(changeIconSize(int)));
00180 
00181 
00182     // Icon chooser button
00183     m_button = new KIconButton(frame);
00184     m_button->setIcon("kexi");
00185     m_button->setIconSize(KIcon::SizeMedium);
00186     l->addMultiCellWidget(m_button, 0, 1, 2, 2);
00187     connect(m_button, SIGNAL(iconChanged(QString)), this, SLOT(updateIconName(QString)));
00188     connect(m_nameInput, SIGNAL(textChanged(const QString &)), this, SLOT(setIcon(const QString &)));
00189 }
00190 
00191 void
00192 LoadIconDialog::updateIconName(QString icon)
00193 {
00194     m_nameInput->setText(icon);
00195 }
00196 
00197 void
00198 LoadIconDialog::setIcon(const QString &icon)
00199 {
00200     m_button->setIcon(icon);
00201 }
00202 
00203 void
00204 LoadIconDialog::changeIconSize(int index)
00205 {
00206     int size = KIcon::SizeMedium;
00207     switch(index)
00208     {
00209         case 0: size = KIcon::SizeSmall; break;
00210         //case 1: size = KIcon::SizeSmallMedium; break;
00211         case 1: size = KIcon::SizeMedium; break;
00212         case 2: size = KIcon::SizeLarge; break;
00213 #if !defined(Q_WS_WIN) && KDE_IS_VERSION(3,1,9)
00214         case 3: size = KIcon::SizeHuge; break;
00215 #endif
00216         default:;
00217     }
00218 
00219     m_button->setIconSize(size);
00220 }
00221 
00222 int LoadIconDialog::iconSize()
00223 {
00224     return m_button->iconSize();
00225 }
00226 
00227 QString LoadIconDialog::iconName()
00228 {
00229     return m_button->icon();
00230 }
00231 
00233 PixmapCollectionEditor::PixmapCollectionEditor(PixmapCollection *collection, QWidget *parent)
00234 : KDialogBase(parent, "pixcollection_dialog", true,
00235     i18n("Edit Pixmap Collection: %1").arg(collection->collectionName()), Close, Close, false)
00236 {
00237     m_collection = collection;
00238     QFrame *frame = makeMainWidget();
00239     QHBoxLayout *l = new QHBoxLayout(frame, 0, 6);
00240     setInitialSize(QSize(400, 200), true);
00241 
00243     QVBoxLayout *vlayout = new QVBoxLayout(l, 3);
00244     QToolButton *newItemPath = new QToolButton(frame);
00245     newItemPath->setIconSet(BarIconSet("fileopen"));
00246     newItemPath->setTextLabel(i18n("&Add File"), true);
00247     vlayout->addWidget(newItemPath);
00248     m_buttons.insert(BNewItemPath, newItemPath);
00249     connect(newItemPath, SIGNAL(clicked()), this, SLOT(newItemByPath()));
00250 
00251     QToolButton *newItemName = new QToolButton(frame);
00252     newItemName->setIconSet(BarIconSet("icons"));
00253     newItemName->setTextLabel(i18n("&Add an Icon"), true);
00254     vlayout->addWidget(newItemName);
00255     m_buttons.insert(BNewItemName, newItemName);
00256     connect(newItemName, SIGNAL(clicked()), this, SLOT(newItemByName()));
00257 
00258     QToolButton *delItem = new QToolButton(frame);
00259     delItem->setIconSet(BarIconSet("edit_remove"));
00260     delItem->setTextLabel(i18n("&Remove Selected Item"), true);
00261     vlayout->addWidget(delItem);
00262     m_buttons.insert(BDelItem, delItem);
00263     connect(delItem, SIGNAL(clicked()), this, SLOT(removeItem()));
00264     vlayout->addStretch();
00265 
00266     // Setup the iconView
00267     m_iconView = new KIconView(frame, "pixcollection_iconView");
00268     m_iconView->resize(100,100);
00269     m_iconView->setArrangement(QIconView::LeftToRight);
00270     m_iconView->setAutoArrange(true);
00271     m_iconView->setMode(KIconView::Select);
00272     l->addWidget(m_iconView);
00273     connect(m_iconView, SIGNAL(contextMenuRequested(QIconViewItem*, const QPoint&)), this, SLOT(displayMenu(QIconViewItem*, const QPoint&)));
00274     connect(m_iconView, SIGNAL(itemRenamed(QIconViewItem*, const QString &)), this, SLOT(renameCollectionItem(QIconViewItem*, const QString&)));
00275 
00276     PixmapMap::ConstIterator it;
00277     PixmapMap::ConstIterator endIt = collection->m_pixmaps.end();
00278     for(it = collection->m_pixmaps.constBegin(); it != endIt; ++it)
00279         createIconViewItem(it.key());
00280 }
00281 
00282 void
00283 PixmapCollectionEditor::newItemByName()
00284 {
00285     LoadIconDialog d(parentWidget());
00286     if(d.exec()== QDialog::Accepted)
00287     {
00288         if(d.iconName().isEmpty())
00289             return;
00290 
00291         QString name = m_collection->addPixmapName(d.iconName(), d.iconSize());
00292         createIconViewItem(name);
00293     }
00294 }
00295 
00296 void
00297 PixmapCollectionEditor::newItemByPath()
00298 {
00299     KURL url = KFileDialog::getImageOpenURL("::kexi", parentWidget());
00300     if(url.isEmpty())
00301         return;
00302     QString name = m_collection->addPixmapPath(url);
00303     createIconViewItem(name);
00304 }
00305 
00306 void
00307 PixmapCollectionEditor::removeItem()
00308 {
00309     QIconViewItem *item = m_iconView->currentItem();
00310     if( !item )
00311       return;
00312 
00313     int confirm = KMessageBox::questionYesNo(parentWidget(), QString("<qt>")+
00314         i18n("Do you want to remove item \"%1\" from collection \"%2\"?")
00315         .arg(item->text()).arg(m_collection->collectionName()) + "</qt>");
00316     if(confirm == KMessageBox::No)
00317         return;
00318 
00319     m_collection->removePixmap(item->text());
00320     delete item;
00321 }
00322 
00323 void
00324 PixmapCollectionEditor::renameItem()
00325 {
00326         if(m_iconView->currentItem())
00327                 m_iconView->currentItem()->rename();
00328 }
00329 
00330 void
00331 PixmapCollectionEditor::createIconViewItem(const QString &name)
00332 {
00333     PixmapIconViewItem *item = new PixmapIconViewItem(m_iconView, name, getPixmap(name));
00334     item->setRenameEnabled(true);
00335 }
00336 
00337 QPixmap
00338 PixmapCollectionEditor::getPixmap(const QString &name)
00339 {
00340     QPixmap pixmap = m_collection->getPixmap(name);
00341     if((pixmap.width() <= 48) && (pixmap.height() <= 48))
00342         return pixmap;
00343 
00344     KPixmapIO io;
00345     QImage image = io.convertToImage(pixmap);
00346     pixmap = io.convertToPixmap(image.scale(48, 48, QImage::ScaleMin));
00347     return pixmap;
00348 }
00349 
00350 void
00351 PixmapCollectionEditor::renameCollectionItem(QIconViewItem *it, const QString &name)
00352 {
00353     PixmapIconViewItem *item = static_cast<PixmapIconViewItem*>(it);
00354     if(!m_collection->m_pixmaps.contains(item->name()))
00355         return;
00356 
00357     // We just rename the collection item
00358     QPair<QString, int> pair = m_collection->m_pixmaps[item->name()];
00359     m_collection->m_pixmaps.remove(item->name());
00360     m_collection->m_pixmaps[name] = pair;
00361     item->setName(name);
00362 }
00363 
00364 void
00365 PixmapCollectionEditor::displayMenu(QIconViewItem *it, const QPoint &p)
00366 {
00367     if(!it) return;
00368     KPopupMenu *menu = new KPopupMenu();
00369     menu->insertItem(SmallIconSet("edit"), i18n("Rename Item"), this, SLOT(renameItem()));
00370     menu->insertItem(SmallIconSet("remove"), i18n("Remove Item"), this, SLOT(removeItem()));
00371     menu->exec(p);
00372 }
00373 
00375 PixmapCollectionChooser::PixmapCollectionChooser(PixmapCollection *collection, const QString &selectedItem, QWidget *parent)
00376 : KDialogBase(parent, "pixchoose_dialog", true, i18n("Select Pixmap From %1").arg(collection->collectionName()),
00377    User1|Ok|Cancel, Ok, false, KGuiItem(i18n("Edit Collection...")))
00378 {
00379     m_collection = collection;
00380     setInitialSize(QSize(400, 200), true);
00381 
00382     m_iconView = new KIconView(this, "pixchooser_iconView");
00383     setMainWidget(m_iconView);
00384     m_iconView->setArrangement(QIconView::LeftToRight);
00385     m_iconView->setAutoArrange(true);
00386     m_iconView->setMode(KIconView::Select);
00387 
00388     PixmapMap::ConstIterator it;
00389     PixmapMap::ConstIterator endIt = collection->m_pixmaps.constEnd();
00390     for(it = collection->m_pixmaps.constBegin(); it != endIt; ++it)
00391         new PixmapIconViewItem(m_iconView, it.key(), getPixmap(it.key()));
00392 
00393     QIconViewItem *item = m_iconView->findItem(selectedItem, Qt::ExactMatch);
00394     if(item && !selectedItem.isEmpty())
00395         m_iconView->setCurrentItem(item);
00396 }
00397 
00398 QPixmap
00399 PixmapCollectionChooser::pixmap()
00400 {
00401         if(! m_iconView->currentItem())
00402                 return QPixmap();
00403     QString name = m_iconView->currentItem()->text();
00404     return m_collection->getPixmap(name);
00405 }
00406 
00407 QString
00408 PixmapCollectionChooser::pixmapName()
00409 {
00410     return m_iconView->currentItem() ? m_iconView->currentItem()->text() : QString("");
00411 }
00412 
00413 QPixmap
00414 PixmapCollectionChooser::getPixmap(const QString &name)
00415 {
00416     QPixmap pixmap = m_collection->getPixmap(name);
00417     if((pixmap.width() <= 48) && (pixmap.height() <= 48))
00418         return pixmap;
00419 
00420     // We scale the pixmap down to 48x48 to fit in the iconView
00421     KPixmapIO io;
00422     QImage image = io.convertToImage(pixmap);
00423     pixmap = io.convertToPixmap(image.scale(48, 48, QImage::ScaleMin));
00424     return pixmap;
00425 }
00426 
00427 void
00428 PixmapCollectionChooser::slotUser1()
00429 {
00430     PixmapCollectionEditor dialog(m_collection, parentWidget());
00431     dialog.exec();
00432 
00433     m_iconView->clear();
00434     PixmapMap::ConstIterator it;
00435     PixmapMap::ConstIterator endIt = m_collection->m_pixmaps.constEnd();
00436     for(it = m_collection->m_pixmaps.constBegin(); it != endIt; ++it)
00437          new PixmapIconViewItem(m_iconView, it.key(), getPixmap(it.key()));
00438 }
00439 
00440 #include "pixmapcollection.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys