Kate
kateargumenthinttree.cpp
Go to the documentation of this file.
00001 /* This file is part of the KDE libraries and the Kate part. 00002 * 00003 * Copyright (C) 2007 David Nolden <david.nolden.kdevelop@art-master.de> 00004 * 00005 * This library is free software; you can redistribute it and/or 00006 * modify it under the terms of the GNU Library General Public 00007 * License as published by the Free Software Foundation; either 00008 * version 2 of the License, or (at your option) any later version. 00009 * 00010 * This library is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 * Library General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU Library General Public License 00016 * along with this library; see the file COPYING.LIB. If not, write to 00017 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00018 * Boston, MA 02110-1301, USA. 00019 */ 00020 00021 #include "kateargumenthinttree.h" 00022 00023 #include <QHeaderView> 00024 #include <QApplication> 00025 #include <QDesktopWidget> 00026 #include <QScrollBar> 00027 00028 #include "kateargumenthintmodel.h" 00029 #include "katecompletionwidget.h" 00030 #include "expandingtree/expandingwidgetmodel.h" 00031 #include "katecompletiondelegate.h" 00032 #include "kateview.h" 00033 #include <QModelIndex> 00034 00035 00036 KateArgumentHintTree::KateArgumentHintTree( KateCompletionWidget* parent ) : ExpandingTree(0), m_parent(parent) { //Do not use the completion-widget as widget-parent, because the argument-hint-tree will be rendered separately 00037 00038 setFrameStyle( QFrame::Box | QFrame::Plain ); 00039 setLineWidth( 1 ); 00040 00041 connect( parent, SIGNAL(destroyed(QObject*)), this, SLOT(deleteLater()) ); 00042 setFrameStyle(QFrame::NoFrame); 00043 setFrameStyle( QFrame::Box | QFrame::Plain ); 00044 setFocusPolicy(Qt::NoFocus); 00045 setWindowFlags(Qt::Tool | Qt::FramelessWindowHint); 00046 setUniformRowHeights(false); 00047 setVerticalScrollMode(QAbstractItemView::ScrollPerPixel); 00048 header()->hide(); 00049 setRootIsDecorated(false); 00050 setIndentation(0); 00051 setAllColumnsShowFocus(true); 00052 setAlternatingRowColors(true); 00053 setItemDelegate(new KateCompletionDelegate(parent->argumentHintModel(), parent)); 00054 } 00055 00056 void KateArgumentHintTree::clearCompletion() { 00057 setCurrentIndex(QModelIndex()); 00058 } 00059 00060 KateArgumentHintModel* KateArgumentHintTree::model() const { 00061 return m_parent->argumentHintModel(); 00062 } 00063 00064 void KateArgumentHintTree::paintEvent ( QPaintEvent * event ) { 00065 QTreeView::paintEvent(event); 00066 updateGeometry(); 00067 } 00068 00069 void KateArgumentHintTree::dataChanged ( const QModelIndex & topLeft, const QModelIndex & bottomRight ) { 00070 QTreeView::dataChanged(topLeft,bottomRight); 00071 //updateGeometry(); 00072 } 00073 00074 void KateArgumentHintTree::currentChanged ( const QModelIndex & current, const QModelIndex & previous ) { 00075 /* kDebug( 13035 ) << "currentChanged()";*/ 00076 static_cast<ExpandingWidgetModel*>(model())->rowSelected(current); 00077 QTreeView::currentChanged(current, previous); 00078 } 00079 00080 void KateArgumentHintTree::rowsInserted ( const QModelIndex & parent, int start, int end ) { 00081 QTreeView::rowsInserted(parent, start, end); 00082 updateGeometry(); 00083 } 00084 00085 int KateArgumentHintTree::sizeHintForColumn(int column) const { 00086 return QTreeView::sizeHintForColumn(column); 00087 } 00088 00089 unsigned int KateArgumentHintTree::rowHeight(const QModelIndex& index) const { 00090 uint max = sizeHintForIndex(index).height(); 00091 00092 for(int a = 0; a < index.model()->columnCount(index.parent()); ++a) { 00093 QModelIndex i = index.sibling(index.row(), a); 00094 uint cSize = sizeHintForIndex(i).height(); 00095 if(cSize > max) 00096 max = cSize; 00097 } 00098 return max; 00099 } 00100 00101 void KateArgumentHintTree::updateGeometry(QRect geom) { 00102 //Avoid recursive calls of updateGeometry 00103 static bool updatingGeometry = false; 00104 if( updatingGeometry ) return; 00105 updatingGeometry = true; 00106 00107 if( model()->rowCount(QModelIndex()) == 0 ) { 00108 /* kDebug( 13035 ) << "KateArgumentHintTree:: empty model";*/ 00109 hide(); 00110 setGeometry(geom); 00111 updatingGeometry = false; 00112 return; 00113 } 00114 00115 int bottom = geom.bottom(); 00116 int totalWidth = resizeColumns(); 00117 int totalHeight = 0; 00118 for(int a = 0; a < model()->rowCount( QModelIndex() ); ++a) { 00119 QModelIndex index(model()->index(a, 0)); 00120 totalHeight += rowHeight(index); 00121 for(int b = 0; b < model()->rowCount(index); ++b) { 00122 QModelIndex childIndex = index.child(b, 0); 00123 totalHeight += rowHeight(childIndex); 00124 } 00125 } 00126 00127 totalHeight += frameWidth()*2; 00128 00129 QRect topRect = visualRect(model()->index(0, 0)); 00130 QRect contentRect = visualRect(model()->index(model()->rowCount(QModelIndex())-1, 0)); 00131 00132 geom.setHeight(totalHeight); 00133 00134 geom.moveBottom(bottom); 00135 // if( totalWidth > geom.width() ) 00136 geom.setWidth(totalWidth); 00137 00138 bool enableScrollBars = false; 00139 00140 //Resize and move so it fits the screen horizontally 00141 int maxWidth = (QApplication::desktop()->screenGeometry(m_parent->view()).width()*3)/4; 00142 if( geom.width() > maxWidth ) { 00143 geom.setWidth(maxWidth); 00144 geom.setHeight(geom.height() + horizontalScrollBar()->height() +2); 00145 geom.moveBottom(bottom); 00146 enableScrollBars = true; 00147 } 00148 00149 if (geom.right() > QApplication::desktop()->screenGeometry(m_parent->view()).right()) 00150 geom.moveRight( QApplication::desktop()->screenGeometry(m_parent->view()).right() ); 00151 00152 if( geom.left() < QApplication::desktop()->screenGeometry(m_parent->view()).left() ) 00153 geom.moveLeft(QApplication::desktop()->screenGeometry(m_parent->view()).left()); 00154 00155 //Resize and move so it fits the screen vertically 00156 bool resized = false; 00157 if( geom.top() < QApplication::desktop()->screenGeometry(this).top() ) { 00158 int offset = QApplication::desktop()->screenGeometry(this).top() - geom.top(); 00159 geom.setBottom( geom.bottom() - offset ); 00160 geom.moveTo(geom.left(), QApplication::desktop()->screenGeometry(this).top()); 00161 resized = true; 00162 } 00163 00164 if(geom != geometry()) 00165 { 00166 setUpdatesEnabled(false); 00167 setAnimated(false); 00168 00169 setHorizontalScrollBarPolicy( enableScrollBars ? Qt::ScrollBarAlwaysOn : Qt::ScrollBarAlwaysOff ); 00170 00171 /* kDebug( 13035 ) << "KateArgumentHintTree::updateGeometry: updating geometry to " << geom;*/ 00172 setGeometry(geom); 00173 00174 if( resized && currentIndex().isValid() ) 00175 scrollTo(currentIndex()); 00176 00177 setUpdatesEnabled(true); 00178 } 00179 00180 updatingGeometry = false; 00181 } 00182 00183 int KateArgumentHintTree::resizeColumns() { 00184 int totalSize = 0; 00185 for( int a = 0; a < header()->count(); a++ ) { 00186 int columnSize = sizeHintForColumn(a); 00187 setColumnWidth(a, columnSize); 00188 totalSize += columnSize; 00189 } 00190 return totalSize; 00191 } 00192 00193 void KateArgumentHintTree::updateGeometry() { 00194 updateGeometry( geometry() ); 00195 } 00196 00197 bool KateArgumentHintTree::nextCompletion() 00198 { 00199 QModelIndex current; 00200 QModelIndex firstCurrent = currentIndex(); 00201 00202 do { 00203 QModelIndex oldCurrent = currentIndex(); 00204 00205 current = moveCursor(MoveDown, Qt::NoModifier); 00206 00207 if (current != oldCurrent && current.isValid()) { 00208 setCurrentIndex(current); 00209 00210 } else { 00211 if (firstCurrent.isValid()) 00212 setCurrentIndex(firstCurrent); 00213 return false; 00214 } 00215 00216 } while (!model()->indexIsItem(current)); 00217 00218 return true; 00219 } 00220 00221 bool KateArgumentHintTree::previousCompletion() 00222 { 00223 QModelIndex current; 00224 QModelIndex firstCurrent = currentIndex(); 00225 00226 do { 00227 QModelIndex oldCurrent = currentIndex(); 00228 00229 current = moveCursor(MoveUp, Qt::NoModifier); 00230 00231 if (current != oldCurrent && current.isValid()) { 00232 setCurrentIndex(current); 00233 00234 } else { 00235 if (firstCurrent.isValid()) 00236 setCurrentIndex(firstCurrent); 00237 return false; 00238 } 00239 00240 } while (!model()->indexIsItem(current)); 00241 00242 return true; 00243 } 00244 00245 bool KateArgumentHintTree::pageDown( ) 00246 { 00247 QModelIndex old = currentIndex(); 00248 QModelIndex current = moveCursor(MovePageDown, Qt::NoModifier); 00249 00250 if (current.isValid()) { 00251 setCurrentIndex(current); 00252 if (!model()->indexIsItem(current)) 00253 if (!nextCompletion()) 00254 previousCompletion(); 00255 } 00256 00257 return current != old; 00258 } 00259 00260 bool KateArgumentHintTree::pageUp( ) 00261 { 00262 QModelIndex old = currentIndex(); 00263 QModelIndex current = moveCursor(MovePageUp, Qt::NoModifier); 00264 00265 if (current.isValid()) { 00266 setCurrentIndex(current); 00267 if (!model()->indexIsItem(current)) 00268 if (!previousCompletion()) 00269 nextCompletion(); 00270 } 00271 return current != old; 00272 } 00273 00274 void KateArgumentHintTree::top( ) 00275 { 00276 QModelIndex current = moveCursor(MoveHome, Qt::NoModifier); 00277 setCurrentIndex(current); 00278 00279 if (current.isValid()) { 00280 setCurrentIndex(current); 00281 if (!model()->indexIsItem(current)) 00282 nextCompletion(); 00283 } 00284 } 00285 00286 void KateArgumentHintTree::bottom( ) 00287 { 00288 QModelIndex current = moveCursor(MoveEnd, Qt::NoModifier); 00289 setCurrentIndex(current); 00290 00291 if (current.isValid()) { 00292 setCurrentIndex(current); 00293 if (!model()->indexIsItem(current)) 00294 previousCompletion(); 00295 } 00296 } 00297 00298 #include "kateargumenthinttree.moc"
KDE 4.6 API Reference