1/*
2 * Copyright (C) 2000 Matthias Elter <elter@kde.org>
3 * Copyright (C) 2001-2002 Raffaele Sandrini <sandrini@kde.org>
4 * Copyright (C) 2003 Waldo Bastian <bastian@kde.org>
5 * Copyright (C) 2008 Laurent Montel <montel@kde.org>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 *
21 */
22
23#ifndef treeview_h
24#define treeview_h
25
26#include <QTreeWidget>
27#include <QMimeData>
28
29#include <KService>
30#include <KServiceGroup>
31
32class QMenu;
33class QDropEvent;
34class QSignalMapper;
35
36class KActionCollection;
37class KDesktopFile;
38class MenuFile;
39class MenuFolderInfo;
40class MenuEntryInfo;
41class MenuSeparatorInfo;
42class KShortcut;
43
44static const QString SAVE_ACTION_NAME = "file_save";
45static const QString NEW_ITEM_ACTION_NAME = "new_item";
46static const QString NEW_SUBMENU_ACTION_NAME = "new_submenu";
47static const QString NEW_SEPARATOR_ACTION_NAME = "new_separator";
48static const QString CUT_ACTION_NAME = "edit_cut";
49static const QString COPY_ACTION_NAME = "edit_copy";
50static const QString PASTE_ACTION_NAME = "edit_paste";
51static const QString DELETE_ACTION_NAME = "delete";
52static const QString SORT_ACTION_NAME = "sort";
53static const QString SORT_BY_NAME_ACTION_NAME = "sort_by_name";
54static const QString SORT_BY_DESCRIPTION_ACTION_NAME = "sort_by_description";
55static const QString SORT_ALL_BY_NAME_ACTION_NAME = "sort_all_by_name";
56static const QString SORT_ALL_BY_DESCRIPTION_ACTION_NAME = "sort_all_by_description";
57static const QString MOVE_UP_ACTION_NAME = "move_up";
58static const QString MOVE_DOWN_ACTION_NAME = "move_down";
59
60class TreeItem : public QTreeWidgetItem
61{
62public:
63 TreeItem(QTreeWidgetItem *parent, QTreeWidgetItem *after, const QString &menuId, bool __init = false);
64 TreeItem(QTreeWidget *parent, QTreeWidgetItem *after, const QString &menuId, bool __init = false);
65 ~TreeItem();
66 static bool itemNameLessThan(QTreeWidgetItem *item1, QTreeWidgetItem *item2);
67 static bool itemDescriptionLessThan(QTreeWidgetItem *item1, QTreeWidgetItem *item2);
68
69 QString menuId() const { return m_menuId; }
70
71 QString directory() const { return m_directoryPath; }
72 void setDirectoryPath(const QString& path) { m_directoryPath = path; }
73
74 MenuFolderInfo *folderInfo() { return m_folderInfo; }
75 void setMenuFolderInfo(MenuFolderInfo *folderInfo) { m_folderInfo = folderInfo; }
76
77 MenuEntryInfo *entryInfo() const { return m_entryInfo; }
78 void setMenuEntryInfo(MenuEntryInfo *entryInfo) { m_entryInfo = entryInfo; }
79
80 QString name() const { return m_name; }
81 void setName(const QString &name);
82
83 QString description() const;
84
85 bool isDirectory() const { return m_folderInfo; }
86 bool isEntry() const { return m_entryInfo; }
87 bool isSeparator() const { return !isDirectory() && !isEntry(); }
88
89 bool isHiddenInMenu() const { return m_hidden; }
90 void setHiddenInMenu(bool b);
91
92 bool isLayoutDirty() const;
93 void setLayoutDirty() { m_layoutDirty = true; }
94 void saveLayout(MenuFile *menuFile);
95
96 void load();
97
98 //virtual void paintCell(QPainter * p, const QColorGroup & cg, int column, int width, int align);
99
100private:
101 void update();
102
103 bool m_hidden : 1;
104 bool m_init : 1;
105 bool m_layoutDirty : 1;
106 QString m_menuId;
107 QString m_name;
108 QString m_directoryPath;
109 MenuFolderInfo *m_folderInfo;
110 MenuEntryInfo *m_entryInfo;
111};
112
113class TreeView : public QTreeWidget
114{
115 friend class TreeItem;
116 Q_OBJECT
117public:
118 explicit TreeView(KActionCollection *ac, QWidget *parent=0, const char *name=0);
119 ~TreeView();
120
121 void readMenuFolderInfo(MenuFolderInfo *folderInfo=0, KServiceGroup::Ptr folder=KServiceGroup::Ptr(), const QString &prefix=QString());
122 void setViewMode(bool showHidden);
123 bool save();
124
125 bool dirty();
126
127 void selectMenu(const QString &menu);
128 void selectMenuEntry(const QString &menuEntry);
129
130 void restoreMenuSystem();
131
132 void updateTreeView(bool showHidden);
133
134public Q_SLOTS:
135 void currentDataChanged(MenuFolderInfo *folderInfo);
136 void currentDataChanged(MenuEntryInfo *entryInfo);
137 void findServiceShortcut(const KShortcut&, KService::Ptr &);
138
139Q_SIGNALS:
140 void entrySelected(MenuFolderInfo *folderInfo);
141 void entrySelected(MenuEntryInfo *entryInfo);
142 void disableAction();
143
144protected Q_SLOTS:
145 void itemSelected(QTreeWidgetItem *);
146 bool dropMimeData(QTreeWidgetItem *parent, int index, const QMimeData *data, Qt::DropAction action);
147
148 void newsubmenu();
149 void newitem();
150 void newsep();
151
152 void cut();
153 void copy();
154 void paste();
155 void del();
156 void sort(const int sortCmd);
157 void moveUpItem();
158 void moveDownItem();
159
160protected:
161 enum SortType {
162 SortByName = 0,
163 SortByDescription,
164 SortAllByName,
165 SortAllByDescription
166 };
167
168 void contextMenuEvent(QContextMenuEvent *event);
169 void dropEvent(QDropEvent *event);
170 void startDrag(Qt::DropActions supportedActions);
171 QTreeWidgetItem *selectedItem();
172 TreeItem *createTreeItem(TreeItem *parent, QTreeWidgetItem *after, MenuFolderInfo *folderInfo, bool _init = false);
173 TreeItem *createTreeItem(TreeItem *parent, QTreeWidgetItem *after, MenuEntryInfo *entryInfo, bool _init = false);
174 TreeItem *createTreeItem(TreeItem *parent, QTreeWidgetItem *after, MenuSeparatorInfo *sepInfo, bool _init = false);
175
176 void del(TreeItem *, bool deleteInfo);
177 void fill();
178 void fillBranch(MenuFolderInfo *folderInfo, TreeItem *parent);
179 QString findName(KDesktopFile *df, bool deleted);
180 void sortItem(TreeItem *item, const SortType& sortType);
181 void sortItemChildren(const QList<QTreeWidgetItem*>::iterator& begin, const QList<QTreeWidgetItem*>::iterator& end, const SortType& sortType);
182 TreeItem* getParentItem(QTreeWidgetItem *item) const;
183 void moveUpOrDownItem(bool isMovingUpAction);
184
185 void closeAllItems(QTreeWidgetItem *item);
186 TreeItem *expandPath(TreeItem *item, const QString &path);
187
188 // moving = src will be removed later
189 void copy( bool moving );
190
191 void cleanupClipboard();
192
193 bool isLayoutDirty();
194 void setLayoutDirty(TreeItem *);
195 void saveLayout();
196
197 QStringList fileList(const QString& relativePath);
198 QStringList dirList(const QString& relativePath);
199
200 virtual QStringList mimeTypes() const;
201 QMimeData *mimeData(const QList<QTreeWidgetItem *> items) const;
202 Qt::DropActions supportedDropActions() const;
203
204 void sendReloadMenu();
205
206private:
207 KActionCollection *m_ac;
208 QMenu *m_popupMenu;
209 int m_clipboard;
210 MenuFolderInfo *m_clipboardFolderInfo;
211 MenuEntryInfo *m_clipboardEntryInfo;
212 bool m_showHidden;
213 MenuFile *m_menuFile;
214 MenuFolderInfo *m_rootFolder;
215 MenuSeparatorInfo *m_separator;
216 QStringList m_newMenuIds;
217 QStringList m_newDirectoryList;
218 bool m_layoutDirty;
219 bool m_detailedMenuEntries;
220 bool m_detailedEntriesNamesFirst;
221 QStringList m_dropMimeTypes;
222 QSignalMapper *m_sortSignalMapper;
223};
224
225class MenuItemMimeData : public QMimeData
226{
227public:
228 MenuItemMimeData(TreeItem *item);
229 virtual QStringList formats() const;
230 virtual bool hasFormat(const QString &mimeType) const;
231 TreeItem *item() const;
232
233protected:
234 virtual QVariant retrieveData(const QString &mimeType, QVariant::Type type) const;
235
236private:
237 TreeItem *m_item;
238};
239
240Q_DECLARE_METATYPE(TreeItem *)
241
242#endif
243