2
Copyright 2007 Robert Knight <robertknight@gmail.com>
3
Copyright 2008-2009 Sebastian Sauer <mail@dipe.org>
5
This library is free software; you can redistribute it and/or
6
modify it under the terms of the GNU Library General Public
7
License as published by the Free Software Foundation; either
8
version 2 of the License, or (at your option) any later version.
10
This library is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
Library General Public License for more details.
15
You should have received a copy of the GNU Library General Public License
16
along with this library; see the file COPYING.LIB. If not, write to
17
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18
Boston, MA 02110-1301, USA.
25
#include <QtCore/QModelIndex>
30
class QAbstractItemModel;
36
class UrlItemLauncher;
39
* A view for a QAbstractItemModel which displays the model (set with setModel())
40
* as a hierarchical menu.
42
* When the menu is executed and an item is triggered, the model index associated with the
43
* chosen item can be found by calling indexForAction() with the triggered action. The action
44
* associated with a particular model index can be found using actionForIndex().
46
* MenuView creates actions for parts of the model on demand as the user explores the menu.
47
* The type of action created for leaf items in the tree can be changed by re-implementing
48
* createLeafAction(). When a new action is created or if the corresponding model
49
* index's data changes, updateAction() is called to set the action's properties. This
50
* can be reimplemented in sub-classes to change the appearance of the actions.
52
class MenuView : public KMenu
57
/** Constructs a new menu with the specified @p parent */
58
MenuView(QWidget *parent = 0, const QString &title = QString(), const QIcon &icon = QIcon());
62
/// Options for a model.
64
None, ///< no options.
65
MergeFirstLevel ///< merge the first both levels of items within the model into one hirachy in the menuview.
68
/** Adds a model to display within this menu. */
69
void addModel(QAbstractItemModel *model, ModelOptions options = None, const QString & relativePath = QString());
71
/** Adds a QStandardItem to display within this menu. This menu will take over the ownership of the item. */
72
void addItem(QStandardItem *item);
74
/** Returns the UrlItemLauncher used to handle launching of urls. */
75
UrlItemLauncher *launcher() const;
77
/** Maps an action in the menu to its corresponding index in model() */
78
QModelIndex indexForAction(QAction *action) const;
81
* Maps an index in the model to its corresponding action in the menu.
82
* If @p index is invalid then menuAction() will be returned. If @p index
83
* is in a part of the tree which the user has not yet explored then 0 will
84
* be returned because the menu hierarchy is constructed on-demand as the user
87
QAction *actionForIndex(const QModelIndex& index) const;
90
* Returns true if the passed \p index is a valid QModelIndex and does
91
* represent a QAction. This method is equal to the actionForIndex() method
92
* above except allowing to explicit ask if the QModelIndex is valid and
93
* to indicate that way, that it may the case that the QModelIndex went
94
* out of scope already.
96
bool isValidIndex(const QModelIndex& index) const;
98
/** Sets the column from the model which is used to construct the actions in the menu. */
99
void setColumn(int column);
100
/** Returns the column from the model which is used to construct the actions in the menu. */
103
/** The format type enumeration. */
105
Name = 0, ///< Name only
106
Description, ///< Description only
107
NameDescription, ///< Name (Description)
108
DescriptionName, ///< Description (Name)
109
NameDashDescription ///< Name - Description
111
/** \return the format type. */
112
FormatType formatType() const;
113
/** Set the format type. */
114
void setFormatType(FormatType formattype);
115
/** Set visibility of model title on menu. */
116
void setModelTitleVisible(QAbstractItemModel *model, bool visible);
121
* Creates a new action to represent a leaf index in the tree. A leaf index
122
* is one which does not have children. The default implementation creates a new
123
* QAction with no properties set. updateAction() is immediately called on the
124
* return action to set its text and icon.
126
* @param index The index in the model for which an action should be created
127
* @param parent The object which should be set as the parent of the new action
129
virtual QAction *createLeafAction(const QModelIndex& index, QObject *parent);
132
* Sets the text, icon and other properties of @p action using the data
133
* associated with @p index in the model(). This is called whenever the data for
134
* a range of indexes in the tree is altered.
136
* The default implementation sets the action's text to the index's Qt::DisplayRole data
137
* and the action's icon to the index's Qt::DecorationRole data.
139
virtual void updateAction(QAbstractItemModel *model, QAction *action, const QModelIndex& index);
142
virtual bool eventFilter(QObject *watched, QEvent *event);
146
* An item in the menu got triggered.
148
void actionTriggered(QAction* action);
150
void contextMenuRequested(const QPoint& pos);
154
* Compared to the aboutToShow() this signal will be emitted after the menu
157
void afterBeingHidden();
159
void customContextMenuRequested(QMenu* menu, const QPoint& pos);
162
/// new items have been inserted into the model
163
void rowsInserted(const QModelIndex& parent, int start, int end);
164
/// existing items are about to be removed from the model
165
void rowsAboutToBeRemoved(const QModelIndex& parent, int start, int end);
166
/// data within an item of the model change
167
void dataChanged(const QModelIndex& topLeft, const QModelIndex& bottomRight);
168
/// the model did reset itself and all items are invalid