1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
|
/****************************************************************************
Copyright (C) 2005 - 2011 Filipe AZEVEDO & The Monkey Studio Team
http://monkeystudio.org licensing under the GNU GPL.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
****************************************************************************/
#include "pTreeComboBox.h"
#include <QVBoxLayout>
#include <QTreeView>
#include <QAbstractItemModel>
#include <QComboBox>
#include <QHeaderView>
#include <QStyleOptionComboBox>
#include <QStylePainter>
#include <QMouseEvent>
#include <QApplication>
#include <QDesktopWidget>
#include <QStatusBar>
int recursiveCount( const QModelIndex& it )
{
int j = 0;
if ( !it.parent().isValid() )
j++;
for ( int i = 0; i < it.model()->rowCount( it ); i++ )
{
j++;
if ( it.model()->rowCount( it.child( i, 0 ) ) )
j += recursiveCount( it.child( i, 0 ) );
}
return j;
}
/*!
\details Create a new pTreeComboBox object
\param parent The parent widget
*/
pTreeComboBox::pTreeComboBox( QWidget* parent )
: QWidget( parent ), mFrame( new QFrame( this ) ), mView( 0 ), mModel( 0 ), mForce( false )
{
mFrame->setWindowFlags( Qt::Dialog | Qt::FramelessWindowHint );
QVBoxLayout* vl = new QVBoxLayout( mFrame );
vl->setSpacing( 0 );
vl->setMargin( 3 );
mIconSize = QSize( 16, 16 );
mSizeHint = QComboBox().sizeHint();
setSizePolicy( QSizePolicy( QSizePolicy::Preferred, QSizePolicy::Fixed ) );
setView( new QTreeView );
mView->header()->hide();
mView->resize( mView->viewport()->size() );
mFrame->installEventFilter( this );
}
pTreeComboBox::~pTreeComboBox()
{ delete mView; }
bool pTreeComboBox::eventFilter( QObject* object, QEvent* event )
{
if ( object == mFrame )
{
if ( event->type() == QEvent::WindowDeactivate )
if ( !rect().contains( mapFromGlobal( QCursor::pos() ) ) )
hidePopup();
return QWidget::eventFilter( object, event );
}
QEvent::Type t = event->type();
if ( t == QEvent::Hide )
{
if ( currentIndex() != mIndex )
{
mForce = false;
mView->clearSelection();
mView->setCurrentIndex( mIndex );
mForce = true;
}
update();
}
else if ( t == QEvent::MouseMove )
{
if ( QMouseEvent* me = static_cast<QMouseEvent*>( event ) )
{
QModelIndex i = mView->indexAt( mView->mapFromGlobal( me->globalPos() ) );
if ( mView->currentIndex() != i )
{
emit highlighted( i );
mForce = false;
mView->clearSelection();
mView->setCurrentIndex( i );
mForce = true;
}
}
}
return QWidget::eventFilter( object, event );
}
/*!
\details Return the widget size hint
*/
QSize pTreeComboBox::sizeHint() const
{ return mSizeHint; }
/*!
\details Return the number of row in the model
*/
int pTreeComboBox::count() const
{ return recursiveCount( mModel->index( 0, 0 ) ); }
/*!
\details Return the iconSize
*/
QSize pTreeComboBox::iconSize() const
{ return mIconSize; }
/*!
\details Set the icon size
*/
void pTreeComboBox::setIconSize( const QSize& s )
{
if ( mIconSize != s )
{
mIconSize = s;
update();
}
}
void pTreeComboBox::paintEvent( QPaintEvent* /*event*/ )
{
QStyleOptionComboBox o;
// QStyleOption
o.initFrom( this );
// QStyleOptionComplex
o.activeSubControls = 0;
o.subControls = QStyle::SC_ComboBoxEditField | QStyle::SC_ComboBoxArrow | QStyle::SC_ComboBoxFrame;
// QStyleOptionComboBox
QModelIndex i = currentIndex();
o.currentIcon = i.data( Qt::DecorationRole ).value<QIcon>();
o.iconSize = mView && mView->iconSize() != QSize( -1, -1 ) ? mView->iconSize() : iconSize();
o.currentText = i.data( Qt::DisplayRole ).toString();
o.editable = false;
o.frame = true;
o.popupRect = QRect();
if ( !mFrame->isVisible() && rect().contains( mapFromGlobal( QCursor::pos() ) ) )
o.state |= QStyle::State_MouseOver;
if ( mFrame->isVisible() )
o.state |= QStyle::State_On;
QStylePainter p( this );
p.drawComplexControl( QStyle::CC_ComboBox, o );
p.drawControl( QStyle::CE_ComboBoxLabel, o );
}
void pTreeComboBox::hideEvent( QHideEvent* /*event*/ )
{ hidePopup(); }
void pTreeComboBox::enterEvent( QEvent* /*event*/ )
{ update(); }
void pTreeComboBox::leaveEvent( QEvent* /*event*/ )
{ update(); }
void pTreeComboBox::mousePressEvent( QMouseEvent* /*event*/ )
{
if ( !mView )
return;
mFrame->isVisible() ? hidePopup() : showPopup();
}
/*!
\details Hide the popup
*/
void pTreeComboBox::hidePopup()
{
if ( mFrame->isVisible() )
mFrame->hide();
}
void pTreeComboBox::calculPopupGeometry()
// code copied from QComboBox original class from Trolltech, arrange to feet my needs
{
//int itemHeight = mView->sizeHintForIndex( mModel->index( 0, 0 ) ).height();
QRect listRect( rect() );
//listRect.setHeight(itemHeight * count() +( 2 *mFrame->layout()->spacing() ) +( 2 *mFrame->frameWidth() ) );
QRect screen = QApplication::desktop()->screenGeometry(this);
QPoint below = mapToGlobal(listRect.bottomLeft());
int belowHeight = screen.bottom() - below.y();
QPoint above = mapToGlobal(listRect.topLeft());
int aboveHeight = above.y() - screen.y();
// make sure the widget fits on screen
if (listRect.width() > screen.width() )
listRect.setWidth(screen.width());
if (mapToGlobal(listRect.bottomRight()).x() > screen.right()) {
below.setX(screen.x() + screen.width() - listRect.width());
above.setX(screen.x() + screen.width() - listRect.width());
}
if (mapToGlobal(listRect.topLeft()).x() < screen.x() ) {
below.setX(screen.x());
above.setX(screen.x());
}
if (listRect.height() <= belowHeight) {
listRect.moveTopLeft(below);
} else if (listRect.height() <= aboveHeight) {
listRect.moveBottomLeft(above);
} else if (belowHeight >= aboveHeight) {
listRect.setHeight(belowHeight);
listRect.moveTopLeft(below);
} else {
listRect.setHeight(aboveHeight);
listRect.moveBottomLeft(above);
}
mFrame->setGeometry( listRect );
}
/*!
\details Show the popup
*/
void pTreeComboBox::showPopup()
{
if ( !mFrame->isVisible() && mView )
{
mIndex = currentIndex();
calculPopupGeometry();
mFrame->show();
update();
}
}
/*!
\details Return the view widget
*/
QTreeView* pTreeComboBox::view() const
{ return mView; }
/*!
\details Set the view widget
*/
void pTreeComboBox::setView( QTreeView* view )
{
if ( mView == view )
return;
delete mView;
mView = view;
if ( mView )
{
if ( mFrame->layout()->count() )
qobject_cast<QVBoxLayout*>( mFrame->layout() )->insertWidget( 0, mView );
else
{
mFrame->layout()->addWidget( mView );
QStatusBar* sb = new QStatusBar( mFrame );
sb->setFixedHeight( 16 );
mFrame->layout()->addWidget( sb );
}
mView->setEditTriggers( QAbstractItemView::NoEditTriggers );
mView->setMouseTracking( true );
mView->viewport()->installEventFilter( this );
setModel( mModel );
connect( mView, SIGNAL( activated( const QModelIndex& ) ), this, SLOT( internal_activated( const QModelIndex& ) ) );
connect( mView, SIGNAL( clicked( const QModelIndex& ) ), this, SLOT( internal_clicked( const QModelIndex& ) ) );
}
}
/*!
\details Return the used model
*/
QAbstractItemModel* pTreeComboBox::model() const
{ return mModel; }
/*!
\details Set the model
*/
void pTreeComboBox::setModel( QAbstractItemModel* model )
{
if ( mModel != model )
mModel = model;
if ( mView && mView->model() != mModel )
{
mView->setModel( mModel );
connect( mView->selectionModel(), SIGNAL( currentChanged( const QModelIndex&, const QModelIndex& ) ), this, SLOT( internal_currentChanged( const QModelIndex&, const QModelIndex& ) ) );
}
}
/*!
\details Return the root index
*/
QModelIndex pTreeComboBox::rootIndex() const
{ return mView ? mView->rootIndex() : QModelIndex(); }
/*!
\details Set the root index
*/
void pTreeComboBox::setRootIndex( const QModelIndex& index )
{ if ( mView ) mView->setRootIndex( index ); }
/*!
\details Return the current index
*/
QModelIndex pTreeComboBox::currentIndex() const
{
if ( mView )
return mFrame->isVisible() ? mIndex : mView->currentIndex();
return QModelIndex();
}
/*!
\details Set the current index
*/
void pTreeComboBox::setCurrentIndex( const QModelIndex& index )
{
if ( mView && ( currentIndex() != index || !index.isValid() ) )
{
mIndex = index;
mForce = true;
mView->clearSelection();
mView->setCurrentIndex( index );
mForce = false;
update();
}
}
/*!
\details Expand all rows
*/
void pTreeComboBox::expandAll()
{ if ( mView ) mView->expandAll(); }
void pTreeComboBox::internal_activated( const QModelIndex& index )
{
if ( !( index.flags() & Qt::ItemIsEnabled ) || !( index.flags() & Qt::ItemIsSelectable ) )
{
return;
}
if ( mIndex != index )
{
mIndex = index;
emit currentChanged( index );
}
emit activated( index );
hidePopup();
}
void pTreeComboBox::internal_clicked( const QModelIndex& index )
{
if ( !( index.flags() & Qt::ItemIsEnabled ) || !( index.flags() & Qt::ItemIsSelectable ) )
{
return;
}
if ( mIndex != index )
{
mIndex = index;
emit currentChanged( index );
}
emit clicked( index );
hidePopup();
}
void pTreeComboBox::internal_currentChanged( const QModelIndex& current, const QModelIndex& )
{
if ( mForce )
emit currentChanged( current );
}
|