Kate
katecompletionmodel.h
Go to the documentation of this file.
00001 /* This file is part of the KDE libraries and the Kate part. 00002 * 00003 * Copyright (C) 2005-2006 Hamish Rodda <rodda@kde.org> 00004 * Copyright (C) 2007-2008 David Nolden <david.nolden.kdevelop@art-master.de> 00005 * 00006 * This library is free software; you can redistribute it and/or 00007 * modify it under the terms of the GNU Library General Public 00008 * License as published by the Free Software Foundation; either 00009 * version 2 of the License, or (at your option) any later version. 00010 * 00011 * This library is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 * Library General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU Library General Public License 00017 * along with this library; see the file COPYING.LIB. If not, write to 00018 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00019 * Boston, MA 02110-1301, USA. 00020 */ 00021 00022 #ifndef KATECOMPLETIONMODEL_H 00023 #define KATECOMPLETIONMODEL_H 00024 00025 #include <QtGui/QAbstractProxyModel> 00026 #include <QtCore/QPair> 00027 #include <QtCore/QList> 00028 #include <QPersistentModelIndex> 00029 00030 #include <ktexteditor/codecompletionmodel.h> 00031 00032 #include "katepartprivate_export.h" 00033 #include "expandingtree/expandingwidgetmodel.h" 00034 00035 class KateCompletionWidget; 00036 class KateArgumentHintModel; 00037 class KateView; 00038 class QWidget; 00039 class QTextEdit; 00040 class QTimer; 00041 class HierarchicalModelHandler; 00042 00049 class KATEPART_TESTS_EXPORT KateCompletionModel : public ExpandingWidgetModel 00050 { 00051 Q_OBJECT 00052 00053 public: 00054 KateCompletionModel(KateCompletionWidget* parent = 0L); 00055 ~KateCompletionModel(); 00056 00057 QList<KTextEditor::CodeCompletionModel*> completionModels() const; 00058 void clearCompletionModels(); 00059 void addCompletionModel(KTextEditor::CodeCompletionModel* model); 00060 void setCompletionModel(KTextEditor::CodeCompletionModel* model); 00061 void setCompletionModels(const QList<KTextEditor::CodeCompletionModel*>& models); 00062 void removeCompletionModel(KTextEditor::CodeCompletionModel* model); 00063 00064 KateView* view() const; 00065 KateCompletionWidget* widget() const; 00066 00067 QString currentCompletion(KTextEditor::CodeCompletionModel* model) const; 00068 void setCurrentCompletion(KTextEditor::CodeCompletionModel* model, const QString& completion); 00069 00070 Qt::CaseSensitivity matchCaseSensitivity() const; 00071 void setMatchCaseSensitivity( Qt::CaseSensitivity cs ); 00072 00073 static QString columnName(int column); 00074 int translateColumn(int sourceColumn) const; 00075 00076 static QString propertyName(KTextEditor::CodeCompletionModel::CompletionProperty property); 00077 00080 QString commonPrefix(QModelIndex selectedIndex) const; 00081 00082 virtual void rowSelected(const QModelIndex& row); 00083 00084 virtual bool indexIsItem(const QModelIndex& index) const; 00085 00086 virtual int columnCount ( const QModelIndex & parent = QModelIndex() ) const; 00087 virtual QVariant data ( const QModelIndex & index, int role = Qt::DisplayRole ) const; 00088 virtual Qt::ItemFlags flags ( const QModelIndex & index ) const; 00089 virtual bool hasChildren ( const QModelIndex & parent = QModelIndex() ) const; 00090 virtual bool hasIndex ( int row, int column, const QModelIndex & parent = QModelIndex() ) const; 00091 virtual QModelIndex index ( int row, int column, const QModelIndex & parent = QModelIndex() ) const; 00092 00093 // Disabled in case of bugs, reenable once fully debugged. 00094 //virtual QMap<int, QVariant> itemData ( const QModelIndex & index ) const; 00095 virtual QModelIndex parent ( const QModelIndex & index ) const; 00096 virtual int rowCount ( const QModelIndex & parent = QModelIndex() ) const; 00097 00098 // Disabled in case of bugs, reenable once fully debugged. 00099 //virtual QModelIndex sibling ( int row, int column, const QModelIndex & index ) const; 00100 virtual void sort ( int column, Qt::SortOrder order = Qt::AscendingOrder ); 00101 00103 virtual QModelIndex mapToSource(const QModelIndex &proxyIndex) const; 00104 00106 virtual QModelIndex mapFromSource(const QModelIndex &sourceIndex) const; 00107 00108 // Sorting 00109 bool isSortingEnabled() const; 00110 bool isSortingAlphabetical() const; 00111 bool isSortingByInheritanceDepth() const; 00112 void setSortingByInheritanceDepth(bool byIneritance); 00113 void setSortingAlphabetical(bool alphabetical); 00114 00115 Qt::CaseSensitivity sortingCaseSensitivity() const; 00116 void setSortingCaseSensitivity(Qt::CaseSensitivity cs); 00117 00118 bool isSortingReverse() const; 00119 void setSortingReverse(bool reverse); 00120 00121 // Filtering 00122 bool isFilteringEnabled() const; 00123 00124 bool filterContextMatchesOnly() const; 00125 void setFilterContextMatchesOnly(bool filter); 00126 00127 bool filterByAttribute() const; 00128 void setFilterByAttribute(bool filter); 00129 00130 KTextEditor::CodeCompletionModel::CompletionProperties filterAttributes() const; 00131 void setFilterAttributes(KTextEditor::CodeCompletionModel::CompletionProperties attributes); 00132 00133 // A maximum depth of <= 0 equals don't filter by inheritance depth (i.e. infinity) and is default 00134 int maximumInheritanceDepth() const; 00135 void setMaximumInheritanceDepth(int maxDepth); 00136 00137 // Grouping 00138 bool isGroupingEnabled() const; 00139 00140 enum gm { 00141 ScopeType = 0x1, 00142 Scope = 0x2, 00143 AccessType = 0x4, 00144 ItemType = 0x8 00145 }; 00146 00147 enum { //An own property that will be used to mark the best-matches group internally 00148 BestMatchesProperty = 2*KTextEditor::CodeCompletionModel::LastProperty 00149 }; 00150 00151 Q_DECLARE_FLAGS(GroupingMethods, gm) 00152 00153 static const int ScopeTypeMask = 0x380000; 00154 static const int AccessTypeMask = 0x7; 00155 static const int ItemTypeMask = 0xfe0; 00156 00157 GroupingMethods groupingMethod() const; 00158 void setGroupingMethod(GroupingMethods m); 00159 00160 bool accessIncludeConst() const; 00161 void setAccessIncludeConst(bool include); 00162 bool accessIncludeStatic() const; 00163 void setAccessIncludeStatic(bool include); 00164 bool accessIncludeSignalSlot() const; 00165 void setAccessIncludeSignalSlot(bool include); 00166 00167 // Column merging 00168 bool isColumnMergingEnabled() const; 00169 00170 const QList< QList<int> >& columnMerges() const; 00171 void setColumnMerges(const QList< QList<int> >& columnMerges); 00172 00173 void debugStats(); 00174 00176 bool shouldMatchHideCompletionList() const; 00177 00178 uint filteredItemCount() const; 00179 00180 protected: 00181 virtual int contextMatchQuality(const QModelIndex & index) const; 00182 00183 Q_SIGNALS: 00184 void expandIndex(const QModelIndex& index); 00185 //Emitted whenever something has changed about the group of argument-hints 00186 void argumentHintsChanged(); 00187 void contentGeometryChanged(); 00188 00189 public Q_SLOTS: 00190 void setSortingEnabled(bool enable); 00191 void setFilteringEnabled(bool enable); 00192 void setGroupingEnabled(bool enable); 00193 void setColumnMergingEnabled(bool enable); 00194 00195 private Q_SLOTS: 00196 void slotRowsInserted( const QModelIndex & parent, int start, int end ); 00197 void slotRowsRemoved( const QModelIndex & parent, int start, int end ); 00198 void slotModelReset(); 00199 00200 //Updates the best-matches group 00201 void updateBestMatches(); 00202 //Makes sure that the ungrouped group contains each item only once 00203 //Must only be called right after the group was created 00204 void makeGroupItemsUnique(bool onlyFiltered = false); 00205 00206 private: 00207 00208 typedef QPair<KTextEditor::CodeCompletionModel*, QModelIndex> ModelRow; 00209 virtual int contextMatchQuality(const ModelRow& sourceRow) const; 00210 00211 QTreeView* treeView() const; 00212 00213 friend class KateArgumentHintModel; 00214 ModelRow modelRowPair(const QModelIndex& index) const; 00215 00216 // Represents a source row; provides sorting method 00217 class Item { 00218 public: 00219 Item(bool doInitialMatch, KateCompletionModel* model, const HierarchicalModelHandler& handler, ModelRow sourceRow); 00220 00221 bool isValid() const; 00222 // Returns true if the item is not filtered and matches the current completion string 00223 bool isVisible() const; 00224 // Returns whether the item is filtered or not 00225 bool isFiltered() const; 00226 // Returns whether the item matches the current completion string 00227 bool isMatching() const; 00228 00229 bool filter(); 00230 enum MatchType { 00231 NoMatch = 0, 00232 StartsWithMatch, 00233 PerfectMatch 00234 }; 00235 MatchType match(); 00236 00237 const ModelRow& sourceRow() const; 00238 00239 // Sorting operator 00240 bool operator<(const Item& rhs) const; 00241 00242 bool haveExactMatch() const { 00243 return m_haveExactMatch; 00244 } 00245 00246 void clearExactMatch() { 00247 m_haveExactMatch = false; 00248 } 00249 00250 QString name() const { 00251 return m_nameColumn; 00252 } 00253 00254 private: 00255 KateCompletionModel* model; 00256 ModelRow m_sourceRow; 00257 00258 mutable QString m_nameColumn, m_completionSortingName; 00259 00260 int inheritanceDepth; 00261 00262 // True when currently matching completion string 00263 MatchType matchCompletion; 00264 // True when passes all active filters 00265 bool matchFilters, m_haveExactMatch; 00266 00267 QString completionSortingName() const; 00268 }; 00269 00270 public: 00271 // Grouping and sorting of rows 00272 class Group { 00273 public: 00274 explicit Group(KateCompletionModel* model); 00275 00276 void addItem(Item i, bool notifyModel = false); 00278 bool removeItem(const ModelRow& row); 00279 void resort(); 00280 void refilter(); 00281 void clear(); 00282 //Returns whether this group should be ordered before other 00283 bool orderBefore(Group* other) const; 00284 //Returns a number that can be used for ordering 00285 int orderNumber() const; 00286 00290 int rowOf(ModelRow item) { 00291 for(int a = 0; a < filtered.size(); ++a) 00292 if(filtered[a].sourceRow() == item) 00293 return a; 00294 return -1; 00295 } 00296 00297 KateCompletionModel* model; 00298 int attribute; 00299 QString title, scope; 00300 QList<Item> filtered; 00301 QList<Item> prefilter; 00302 bool isEmpty; 00303 //-1 if none was set 00304 int customSortingKey; 00305 }; 00306 00307 bool hasGroups() const; 00308 00309 private: 00310 QString commonPrefixInternal(const QString &forcePrefix) const; 00311 void createGroups(); 00314 QSet<Group*> createItems(const HierarchicalModelHandler&, const QModelIndex& i, bool notifyModel = false); 00317 QSet<Group*> deleteItems(const QModelIndex& i); 00318 Group* createItem(const HierarchicalModelHandler&, const QModelIndex& i, bool notifyModel = false); 00319 void clearGroups(bool reset = true); 00320 void hideOrShowGroup(Group* g, bool notifyModel = true); 00322 Group* fetchGroup(int attribute, const QString& scope = QString(), bool forceGrouping = false); 00323 //If this returns nonzero on an index, the index is the header of the returned group 00324 Group* groupForIndex(const QModelIndex& index) const; 00325 inline Group* groupOfParent(const QModelIndex& child) const { return static_cast<Group*>(child.internalPointer()); } 00326 QModelIndex indexForRow(Group* g, int row) const; 00327 QModelIndex indexForGroup(Group* g) const; 00328 00329 enum changeTypes { 00330 Broaden, 00331 Narrow, 00332 Change 00333 }; 00334 00335 //Returns whether the model needs to be reset 00336 bool changeCompletions(Group* g, changeTypes changeType); 00337 00338 bool hasCompletionModel() const; 00339 00341 int groupingAttributes(int attribute) const; 00342 int countBits(int value) const; 00343 00344 void resort(); 00345 void refilter(); 00346 00347 bool m_hasGroups; 00348 00349 // ### Runtime state 00350 // General 00351 QList<KTextEditor::CodeCompletionModel*> m_completionModels; 00352 QMap<KTextEditor::CodeCompletionModel*, QString> m_currentMatch; 00353 Qt::CaseSensitivity m_matchCaseSensitivity; 00354 00355 // Column merging 00356 QList< QList<int> > m_columnMerges; 00357 00358 QTimer* m_updateBestMatchesTimer; 00359 00360 Group* m_ungrouped; 00361 Group* m_argumentHints; //The argument-hints will be passed on to another model, to be shown in another widget 00362 Group* m_bestMatches; //A temporary group used for holding the best matches of all visible items 00363 00364 // Storing the sorted order 00365 QList<Group*> m_rowTable; 00366 QList<Group*> m_emptyGroups; 00367 // Quick access to each specific group (if it exists) 00368 QMultiHash<int, Group*> m_groupHash; 00369 // Maps custom group-names to their specific groups 00370 QHash<QString, Group*> m_customGroupHash; 00371 00372 // ### Configurable state 00373 // Sorting 00374 bool m_sortingEnabled; 00375 bool m_sortingAlphabetical; 00376 bool m_isSortingByInheritance; 00377 Qt::CaseSensitivity m_sortingCaseSensitivity; 00378 QHash< int, QList<int> > m_sortingGroupingOrder; 00379 00380 // Filtering 00381 bool m_filteringEnabled; 00382 bool m_filterContextMatchesOnly; 00383 bool m_filterByAttribute; 00384 KTextEditor::CodeCompletionModel::CompletionProperties m_filterAttributes; 00385 int m_maximumInheritanceDepth; 00386 00387 // Grouping 00388 bool m_groupingEnabled; 00389 GroupingMethods m_groupingMethod; 00390 bool m_accessConst, m_accessStatic, m_accesSignalSlot; 00391 00392 // Column merging 00393 bool m_columnMergingEnabled/*, m_haveExactMatch*/; 00394 }; 00395 00396 Q_DECLARE_OPERATORS_FOR_FLAGS(KateCompletionModel::GroupingMethods) 00397 00398 #endif
KDE 4.6 API Reference