kcustommenueditor.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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
00058
00059 serviceName.replace(QRegExp("&"), "&&");
00060
00061 QPixmap normal = KGlobal::instance()->iconLoader()->loadIcon(s->icon(), KIcon::Small,
00062 0, KIcon::DefaultState, 0L, true);
00063
00064
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 }
00092
00093 void
00094 KCustomMenuEditor::load(KConfigBase *cfg)
00095 {
00096 cfg->setGroup(QString::null);
00097 int count = cfg->readNumEntry("NrOfItems");
00098 QListViewItem *last = 0;
00099 for(int i = 0; i < count; i++)
00100 {
00101 QString entry = cfg->readEntry(QString("Item%1").arg(i+1));
00102 if (entry.isEmpty())
00103 continue;
00104
00105
00106 KService::Ptr menuItem = KService::serviceByDesktopPath( entry );
00107 if (!menuItem)
00108 menuItem = KService::serviceByDesktopName( entry );
00109 if (!menuItem)
00110 menuItem = new KService( entry );
00111
00112 if (!menuItem->isValid())
00113 continue;
00114
00115 QListViewItem *item = new Item(m_listView, menuItem);
00116 item->moveItem(last);
00117 last = item;
00118 }
00119 }
00120
00121 void
00122 KCustomMenuEditor::save(KConfigBase *cfg)
00123 {
00124
00125 QStringList groups = cfg->groupList();
00126 for(QStringList::ConstIterator it = groups.begin();
00127 it != groups.end(); ++it)
00128 {
00129 cfg->deleteGroup(*it);
00130 }
00131
00132 cfg->setGroup(QString::null);
00133 Item * item = (Item *) m_listView->firstChild();
00134 int i = 0;
00135 while(item)
00136 {
00137 i++;
00138 cfg->writeEntry(QString("Item%1").arg(i), item->s->desktopEntryPath());
00139 item = (Item *) item->nextSibling();
00140 }
00141 cfg->writeEntry("NrOfItems", i);
00142 }
00143
00144 void
00145 KCustomMenuEditor::slotNewItem()
00146 {
00147 QListViewItem *item = m_listView->currentItem();
00148
00149 KOpenWithDlg dlg(this);
00150
00151 if (dlg.exec())
00152 {
00153 KService::Ptr s = dlg.service();
00154 if (s && s->isValid())
00155 {
00156 Item *newItem = new Item(m_listView, s);
00157 newItem->moveItem(item);
00158 }
00159 }
00160 }
00161
00162 void
00163 KCustomMenuEditor::slotRemoveItem()
00164 {
00165 QListViewItem *item = m_listView->currentItem();
00166 if (!item)
00167 return;
00168
00169 delete item;
00170 }
00171
00172 void
00173 KCustomMenuEditor::slotMoveUp()
00174 {
00175 QListViewItem *item = m_listView->currentItem();
00176 if (!item)
00177 return;
00178
00179 QListViewItem *searchItem = m_listView->firstChild();
00180 while(searchItem)
00181 {
00182 QListViewItem *next = searchItem->nextSibling();
00183 if (next == item)
00184 {
00185 searchItem->moveItem(item);
00186 break;
00187 }
00188 searchItem = next;
00189 }
00190 }
00191
00192 void
00193 KCustomMenuEditor::slotMoveDown()
00194 {
00195 QListViewItem *item = m_listView->currentItem();
00196 if (!item)
00197 return;
00198
00199 QListViewItem *after = item->nextSibling();
00200 if (!after)
00201 return;
00202
00203 item->moveItem( after );
00204 }
00205
00206 #include "kcustommenueditor.moc"
This file is part of the documentation for kdelibs Version 3.1.0.