kio Library API Documentation

kcustommenueditor.cpp

00001 /*  This file is part of the KDE libraries
00002     Copyright (C) 2002 Waldo Bastian (bastian@kde.org)
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; version 2 
00007     of the License.
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., 59 Temple Place - Suite 330,
00017     Boston, MA 02111-1307, USA.
00018 */
00019 
00020 #include <qhbox.h>
00021 #include <qregexp.h>
00022 #include <qimage.h>
00023 #include <qpushbutton.h>
00024 
00025 #include <kbuttonbox.h>
00026 #include <klocale.h>
00027 #include <kglobal.h>
00028 #include <kiconloader.h>
00029 #include <klistview.h>
00030 #include <kservice.h>
00031 #include <kconfigbase.h>
00032 #include <kopenwith.h>
00033 
00034 #include "kcustommenueditor.h"
00035 
00036 class KCustomMenuEditor::Item : public QListViewItem
00037 {
00038 public:
00039    Item(QListView *parent, KService::Ptr service)
00040      : QListViewItem(parent),
00041        s(service)
00042    {
00043       init();
00044    }
00045 
00046    Item(QListViewItem *parent, KService::Ptr service)
00047      : QListViewItem(parent),
00048        s(service)
00049    {
00050       init();
00051    }
00052 
00053    void init()
00054    {
00055       QString serviceName = s->name();
00056 
00057       // item names may contain ampersands. To avoid them being converted
00058       // to accelators, replace them with two ampersands.
00059       serviceName.replace(QRegExp("&"), "&&");
00060 
00061       QPixmap normal = KGlobal::instance()->iconLoader()->loadIcon(s->icon(), KIcon::Small,
00062                               0, KIcon::DefaultState, 0L, true);
00063 
00064       // make sure they are not larger than 16x16
00065       if (normal.width() > 16 || normal.height() > 16) {
00066           QImage tmp = normal.convertToImage();
00067           tmp = tmp.smoothScale(16, 16);
00068           normal.convertFromImage(tmp);
00069       }
00070       setText(0, serviceName);
00071       setPixmap(0, normal);
00072    }
00073 
00074    KService::Ptr s;
00075 };
00076 
00077 KCustomMenuEditor::KCustomMenuEditor(QWidget *parent)
00078   : KDialogBase(parent, "custommenueditor", true, i18n("Menu Editor"), Ok|Cancel, Ok, true),
00079     m_listView(0)
00080 {
00081    QHBox *page = makeHBoxMainWidget();
00082    m_listView = new KListView(page);
00083    m_listView->addColumn(i18n("Menu"));
00084    m_listView->setFullWidth(true);
00085    m_listView->setSorting(-1);
00086    KButtonBox *buttonBox = new KButtonBox(page, Vertical);
00087    buttonBox->addButton(i18n("New..."), this, SLOT(slotNewItem()));
00088    buttonBox->addButton(i18n("Remove"), this, SLOT(slotRemoveItem()));
00089    buttonBox->addButton(i18n("Move Up"), this, SLOT(slotMoveUp()));
00090    buttonBox->addButton(i18n("Move Down"), this, SLOT(slotMoveDown()));
00091    buttonBox->layout();
00092 }
00093 
00094 void 
00095 KCustomMenuEditor::load(KConfigBase *cfg)
00096 {
00097    cfg->setGroup(QString::null);
00098    int count = cfg->readNumEntry("NrOfItems");
00099    QListViewItem *last = 0;
00100    for(int i = 0; i < count; i++)
00101    {
00102       QString entry = cfg->readPathEntry(QString("Item%1").arg(i+1));
00103       if (entry.isEmpty())
00104          continue;
00105 
00106       // Try KSycoca first.
00107       KService::Ptr menuItem = KService::serviceByDesktopPath( entry );
00108       if (!menuItem)
00109          menuItem = KService::serviceByDesktopName( entry );
00110       if (!menuItem)
00111          menuItem = new KService( entry );
00112 
00113       if (!menuItem->isValid())
00114          continue;
00115 
00116       QListViewItem *item = new Item(m_listView, menuItem);
00117       item->moveItem(last);
00118       last = item;
00119    }
00120 }
00121 
00122 void 
00123 KCustomMenuEditor::save(KConfigBase *cfg)
00124 {
00125    // First clear the whole config file.
00126    QStringList groups = cfg->groupList();
00127    for(QStringList::ConstIterator it = groups.begin();
00128       it != groups.end(); ++it)
00129    {
00130       cfg->deleteGroup(*it);
00131    }
00132 
00133    cfg->setGroup(QString::null);
00134    Item * item = (Item *) m_listView->firstChild();
00135    int i = 0;
00136    while(item)
00137    {
00138       i++;
00139       cfg->writeEntry(QString("Item%1").arg(i), item->s->desktopEntryPath());
00140       item = (Item *) item->nextSibling();
00141    }
00142    cfg->writeEntry("NrOfItems", i);
00143 }
00144 
00145 void
00146 KCustomMenuEditor::slotNewItem()
00147 {
00148    QListViewItem *item = m_listView->currentItem();
00149    
00150    KOpenWithDlg dlg(this);
00151    
00152    if (dlg.exec())
00153    {
00154       KService::Ptr s = dlg.service();
00155       if (s && s->isValid())
00156       {
00157          Item *newItem = new Item(m_listView, s);
00158          newItem->moveItem(item);
00159       }
00160    }
00161 }
00162 
00163 void
00164 KCustomMenuEditor::slotRemoveItem()
00165 {
00166    QListViewItem *item = m_listView->currentItem();
00167    if (!item)
00168       return;
00169       
00170    delete item;
00171 }
00172 
00173 void
00174 KCustomMenuEditor::slotMoveUp()
00175 {
00176    QListViewItem *item = m_listView->currentItem();
00177    if (!item)
00178       return;
00179       
00180    QListViewItem *searchItem = m_listView->firstChild();
00181    while(searchItem)
00182    {
00183       QListViewItem *next = searchItem->nextSibling();
00184       if (next == item)
00185       {
00186          searchItem->moveItem(item);
00187          break;
00188       }
00189       searchItem = next;
00190    }      
00191 }
00192 
00193 void
00194 KCustomMenuEditor::slotMoveDown()
00195 {
00196    QListViewItem *item = m_listView->currentItem();
00197    if (!item)
00198       return;
00199 
00200    QListViewItem *after = item->nextSibling();
00201    if (!after)
00202       return;
00203       
00204    item->moveItem( after );
00205 }
00206 
00207 #include "kcustommenueditor.moc"
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Jan 28 13:13:15 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001