| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2008 Jeff Mitchell <[email protected]> * |
| 3 |
* * |
| 4 |
* This program is free software; you can redistribute it and/or modify * |
| 5 |
* it under the terms of the GNU General Public License as published by * |
| 6 |
* the Free Software Foundation; either version 2 of the License, or * |
| 7 |
* (at your option) any later version. * |
| 8 |
* * |
| 9 |
* This program is distributed in the hope that it will be useful, * |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 12 |
* GNU General Public License for more details. * |
| 13 |
* * |
| 14 |
* You should have received a copy of the GNU General Public License * |
| 15 |
* along with this program; if not, write to the * |
| 16 |
* Free Software Foundation, Inc., * |
| 17 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * |
| 18 |
***************************************************************************/ |
| 19 |
|
| 20 |
#include "PopupDropper.h" |
| 21 |
#include "PopupDropper_p.h" |
| 22 |
#include "PopupDropperItem.h" |
| 23 |
|
| 24 |
#include <QtDebug> |
| 25 |
#include <QGraphicsItem> |
| 26 |
#include <QGraphicsScene> |
| 27 |
#include <QAction> |
| 28 |
#include <QMenu> |
| 29 |
#include <QPalette> |
| 30 |
#include <QTimeLine> |
| 31 |
#include <QWidget> |
| 32 |
|
| 33 |
PopupDropperPrivate::PopupDropperPrivate( PopupDropper* parent, bool sa, QWidget* widget ) |
| 34 |
: QObject( parent ) |
| 35 |
, standalone( sa ) |
| 36 |
, widget( widget ) |
| 37 |
, scene( nullptr ) |
| 38 |
, view( nullptr ) |
| 39 |
, fade( PopupDropper::FadeInOut ) |
| 40 |
, fadeHideTimer() |
| 41 |
, fadeShowTimer() |
| 42 |
, fadeInTime( 800 ) |
| 43 |
, fadeOutTime( 300 ) |
| 44 |
, deleteTimer() |
| 45 |
, deleteTimeout( 1000 ) |
| 46 |
, frameMax( 30 ) |
| 47 |
, windowColor( 0, 0, 0, 64 ) |
| 48 |
, windowBackgroundBrush() |
| 49 |
, baseTextColor( Qt::white ) |
| 50 |
, hoveredTextColor( Qt::blue ) |
| 51 |
, hoveredBorderPen() |
| 52 |
, hoveredFillBrush() |
| 53 |
, file() |
| 54 |
, sharedRenderer( nullptr ) |
| 55 |
, horizontalOffset( 30 ) |
| 56 |
, pdiItems() |
| 57 |
, overlayLevel( 1 ) |
| 58 |
, entered( false ) |
| 59 |
, submenuMap() |
| 60 |
, submenu( false ) |
| 61 |
, allItems() |
| 62 |
, quitOnDragLeave( false ) |
| 63 |
, onTop( true ) |
| 64 |
, widgetRect() |
| 65 |
, queuedHide( false ) |
| 66 |
, q( parent ) |
| 67 |
{ |
| 68 |
if( widget ) |
| 69 |
widgetRect = widget->rect(); |
| 70 |
|
| 71 |
windowBackgroundBrush.setColor( windowColor ); |
| 72 |
hoveredBorderPen.setColor( Qt::blue ); |
| 73 |
hoveredBorderPen.setWidth( 2 ); |
| 74 |
hoveredBorderPen.setStyle( Qt::SolidLine ); |
| 75 |
QColor hoveredFillColor = QColor( Qt::blue ); |
| 76 |
hoveredFillColor.setAlpha( 32 ); |
| 77 |
hoveredFillBrush.setColor( hoveredFillColor ); |
| 78 |
hoveredFillBrush.setStyle( Qt::SolidPattern ); |
| 79 |
scene = new QGraphicsScene( ( sa ? nullptr : parent ) ); |
| 80 |
view = new PopupDropperView( parent, scene, ( sa ? nullptr : widget ) ); |
| 81 |
//qDebug() << "on create, view size = " << view->size(); |
| 82 |
deleteTimer.setSingleShot( true ); |
| 83 |
fadeHideTimer.setDirection( QTimeLine::Backward ); |
| 84 |
connect( &fadeHideTimer, &QTimeLine::frameChanged, this, &PopupDropperPrivate::fadeHideTimerFrameChanged ); |
| 85 |
connect( &fadeShowTimer, &QTimeLine::frameChanged, this, &PopupDropperPrivate::fadeShowTimerFrameChanged ); |
| 86 |
connect( &fadeHideTimer, &QTimeLine::finished, this, &PopupDropperPrivate::fadeHideTimerFinished ); |
| 87 |
connect( &fadeShowTimer, &QTimeLine::finished, this, &PopupDropperPrivate::fadeShowTimerFinished ); |
| 88 |
connect( &deleteTimer, &QTimer::timeout, this, &PopupDropperPrivate::deleteTimerFinished ); |
| 89 |
} |
| 90 |
|
| 91 |
PopupDropperPrivate::~PopupDropperPrivate() |
| 92 |
{ |
| 93 |
} |
| 94 |
|
| 95 |
void PopupDropperPrivate::newSceneView( PopupDropper* pud ) |
| 96 |
{ |
| 97 |
scene->deleteLater(); |
| 98 |
scene = new QGraphicsScene( pud ); |
| 99 |
//qDebug() << "new scene width in newSceneView = " << scene->width(); |
| 100 |
view = new PopupDropperView( pud, scene, widget ); |
| 101 |
//qDebug() << "on create, view size = " << view->size(); |
| 102 |
} |
| 103 |
|
| 104 |
void PopupDropperPrivate::setParent( QObject* parent ) |
| 105 |
{ |
| 106 |
QObject::setParent( parent ); |
| 107 |
q = static_cast<PopupDropper*>( parent ); |
| 108 |
} |
| 109 |
|
| 110 |
void PopupDropperPrivate::fadeHideTimerFrameChanged( int frame ) //SLOT |
| 111 |
{ |
| 112 |
if( fadeHideTimer.state() == QTimeLine::Running ) |
| 113 |
{ |
| 114 |
qreal val = ( frame * 1.0 ) / frameMax; |
| 115 |
QColor color = windowColor; |
| 116 |
int alpha = (int)( color.alpha() * val ); |
| 117 |
color.setAlpha( alpha ); |
| 118 |
q->setPalette( color ); |
| 119 |
for( PopupDropperItem* pdi : pdiItems ) |
| 120 |
pdi->setSubitemOpacity( val ); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
void PopupDropperPrivate::fadeShowTimerFrameChanged( int frame ) //SLOT |
| 125 |
{ |
| 126 |
if( fadeShowTimer.state() == QTimeLine::Running ) |
| 127 |
{ |
| 128 |
qreal val = ( frame * 1.0 ) / frameMax; |
| 129 |
QColor color = windowColor; |
| 130 |
int alpha = (int)( color.alpha() * val ); |
| 131 |
color.setAlpha( alpha ); |
| 132 |
q->setPalette( color ); |
| 133 |
for( PopupDropperItem* pdi : pdiItems ) |
| 134 |
pdi->setSubitemOpacity( val ); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
void PopupDropperPrivate::fadeHideTimerFinished() //SLOT |
| 139 |
{ |
| 140 |
view->hide(); |
| 141 |
//qDebug() << "Emitting fadeHideFinished in d pointer " << this; |
| 142 |
Q_EMIT q->fadeHideFinished(); |
| 143 |
} |
| 144 |
|
| 145 |
void PopupDropperPrivate::fadeShowTimerFinished() //SLOT |
| 146 |
{ |
| 147 |
q->setPalette( windowColor ); |
| 148 |
queuedHide = false; |
| 149 |
for( PopupDropperItem* pdi : pdiItems ) |
| 150 |
pdi->setSubitemOpacity( 1.0 ); |
| 151 |
} |
| 152 |
|
| 153 |
void PopupDropperPrivate::dragEntered() |
| 154 |
{ |
| 155 |
//qDebug() << "PopupDropperPrivate::dragEntered"; |
| 156 |
q->updateAllOverlays(); |
| 157 |
} |
| 158 |
|
| 159 |
void PopupDropperPrivate::dragLeft() |
| 160 |
{ |
| 161 |
//qDebug() << "PopupDropperPrivate::dragLeft()"; |
| 162 |
//qDebug() << "PUD to be hidden or not hidden = " << q; |
| 163 |
if( view->entered() && quitOnDragLeave ) |
| 164 |
{ |
| 165 |
view->setAcceptDrops( false ); |
| 166 |
//qDebug() << "View entered, hiding"; |
| 167 |
connect( q, &PopupDropper::fadeHideFinished, q, &PopupDropper::subtractOverlay ); |
| 168 |
q->hide(); |
| 169 |
} |
| 170 |
q->updateAllOverlays(); |
| 171 |
} |
| 172 |
|
| 173 |
void PopupDropperPrivate::startDeleteTimer() |
| 174 |
{ |
| 175 |
if( deleteTimeout == 0 ) |
| 176 |
return; |
| 177 |
view->setEntered( false ); |
| 178 |
//qDebug() << "Starting delete timer"; |
| 179 |
deleteTimer.start( deleteTimeout ); |
| 180 |
} |
| 181 |
|
| 182 |
void PopupDropperPrivate::deleteTimerFinished() //SLOT |
| 183 |
{ |
| 184 |
//qDebug() << "Delete Timer Finished"; |
| 185 |
if( !view->entered() && quitOnDragLeave ) |
| 186 |
{ |
| 187 |
connect( q, &PopupDropper::fadeHideFinished, q, &PopupDropper::subtractOverlay ); |
| 188 |
q->hide(); |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
void PopupDropperPrivate::reposItems() |
| 193 |
{ |
| 194 |
qreal partitionsize, my_min, my_max; |
| 195 |
//qDebug() << "allItems.size() = " << allItems.size(); |
| 196 |
int counter = 0; |
| 197 |
for( int i = 0; i < allItems.size(); i++ ) |
| 198 |
{ |
| 199 |
//qDebug() << "item " << i; |
| 200 |
int verticalmargin = 5; |
| 201 |
partitionsize = scene->height() / pdiItems.size(); //gives partition size...now center in this area |
| 202 |
my_min = ( counter * partitionsize ) + verticalmargin; |
| 203 |
my_max = ( ( counter + 1 ) * partitionsize ) - verticalmargin; |
| 204 |
//qDebug() << "my_min = " << my_min << ", my_max = " << my_max; |
| 205 |
PopupDropperItem* pItem = dynamic_cast<PopupDropperItem*>( allItems.at( i ) ); |
| 206 |
QGraphicsLineItem* qglItem = nullptr; |
| 207 |
if( pItem ) |
| 208 |
{ |
| 209 |
pItem->setPopupDropper( q ); //safety |
| 210 |
//qDebug() << "item " << i << " is a PDI "; |
| 211 |
//If the svgElementRect is too high, resize it to fit |
| 212 |
pItem->svgElementRect().setHeight( my_max - my_min - ( 2 * verticalmargin ) ); |
| 213 |
pItem->setPos( 0, my_min ); |
| 214 |
pItem->borderRectItem()->setRect( 0 - pItem->borderWidth(), 0, scene->width() + 2*pItem->borderWidth(), my_max - my_min ); |
| 215 |
pItem->scaleAndReposSvgItem(); |
| 216 |
pItem->reposTextItem(); |
| 217 |
pItem->reposHoverFillRects(); |
| 218 |
pItem->update(); |
| 219 |
//qDebug() << "size of view frame = " << view->size(); |
| 220 |
++counter; |
| 221 |
} |
| 222 |
else if( ( qglItem = dynamic_cast<QGraphicsLineItem*>( allItems.at( i ) ) ) ) |
| 223 |
{ |
| 224 |
//qDebug() << "item " << i << " is a QGLI"; |
| 225 |
qglItem->setLine( horizontalOffset, (my_max-partitionsize), scene->width() - horizontalOffset, (my_max-partitionsize) ); |
| 226 |
} |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
bool PopupDropperPrivate::amIOnTop( PopupDropperView* pdv ) |
| 231 |
{ |
| 232 |
if( onTop && pdv == view ) |
| 233 |
return true; |
| 234 |
return false; |
| 235 |
} |
| 236 |
|
| 237 |
////////////////////////////////////////////////////////////// |
| 238 |
|
| 239 |
PopupDropper::PopupDropper( QWidget *parent, bool standalone ) |
| 240 |
: QObject( parent ) |
| 241 |
, d( new PopupDropperPrivate( this, standalone, parent ) ) |
| 242 |
{ |
| 243 |
if( !parent ) |
| 244 |
{ |
| 245 |
parent = d->view; |
| 246 |
d->widget = parent; |
| 247 |
} |
| 248 |
QObject::setParent( parent ); |
| 249 |
initOverlay( parent ); |
| 250 |
setColors( d->windowColor, d->baseTextColor, d->hoveredTextColor, d->hoveredBorderPen.color(), d->hoveredFillBrush.color() ); |
| 251 |
d->sharedRenderer = new QSvgRenderer( this ); |
| 252 |
d->overlayLevel = 1; |
| 253 |
//qDebug() << "Popup Dropper created!"; |
| 254 |
} |
| 255 |
|
| 256 |
PopupDropper::~PopupDropper() |
| 257 |
{ |
| 258 |
//qDebug() << "Popup Dropper destroyed!"; |
| 259 |
} |
| 260 |
|
| 261 |
int PopupDropper::overlayLevel() const |
| 262 |
{ |
| 263 |
return d->overlayLevel; |
| 264 |
} |
| 265 |
|
| 266 |
void PopupDropper::initOverlay( QWidget* parent, PopupDropperPrivate* priv ) |
| 267 |
{ |
| 268 |
PopupDropperPrivate *pdp = priv ? priv : d; |
| 269 |
//qDebug() << "PUD Overlay being created, d pointer is " << d; |
| 270 |
pdp->scene->setSceneRect( QRectF( parent->rect() ) ); |
| 271 |
//qDebug() << "Scene width = " << pdp->scene->width(); |
| 272 |
pdp->scene->setItemIndexMethod( QGraphicsScene::NoIndex ); |
| 273 |
pdp->view->setFixedSize( parent->size() ); |
| 274 |
pdp->view->setLineWidth( 0 ); |
| 275 |
pdp->view->setFrameStyle( QFrame::NoFrame ); |
| 276 |
pdp->view->setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff ); |
| 277 |
pdp->view->setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOff ); |
| 278 |
pdp->view->setBackgroundRole( QPalette::Window ); |
| 279 |
pdp->view->setAutoFillBackground( true ); |
| 280 |
pdp->fadeHideTimer.setFrameRange( 0, pdp->frameMax ); |
| 281 |
pdp->fadeHideTimer.setUpdateInterval( 20 ); // 50 fps |
| 282 |
pdp->fadeShowTimer.setFrameRange( 0, pdp->frameMax ); |
| 283 |
pdp->fadeShowTimer.setUpdateInterval( 20 ); // 50 fps |
| 284 |
} |
| 285 |
|
| 286 |
void PopupDropper::addOverlay() |
| 287 |
{ |
| 288 |
d->onTop = false; |
| 289 |
m_viewStack.push( d ); |
| 290 |
PopupDropperPrivate* old_d = d; |
| 291 |
d = new PopupDropperPrivate( this, false, old_d->view ); |
| 292 |
d->sharedRenderer = old_d->sharedRenderer; |
| 293 |
//qDebug() << "Adding overlay: "; |
| 294 |
initOverlay( old_d->view ); |
| 295 |
setColors( d->windowColor, d->baseTextColor, d->hoveredTextColor, d->hoveredBorderPen.color(), d->hoveredFillBrush.color() ); |
| 296 |
d->quitOnDragLeave = true; |
| 297 |
d->overlayLevel = old_d->overlayLevel + 1; |
| 298 |
old_d->view->deactivateHover(); |
| 299 |
} |
| 300 |
|
| 301 |
//note: Used when activating a submenu; does not set default colors, should have done that when creating submenu |
| 302 |
void PopupDropper::addOverlay( PopupDropperPrivate* newD ) |
| 303 |
{ |
| 304 |
d->onTop = false; |
| 305 |
//qDebug() << "right before push, m_viewStack.size() is " << m_viewStack.size(); |
| 306 |
m_viewStack.push( d ); |
| 307 |
//qDebug() << "right after push, m_viewStack.size() is " << m_viewStack.size(); |
| 308 |
PopupDropperPrivate* old_d = d; |
| 309 |
d = newD; |
| 310 |
d->onTop = true; |
| 311 |
d->sharedRenderer = old_d->sharedRenderer; |
| 312 |
d->quitOnDragLeave = true; |
| 313 |
d->overlayLevel = old_d->overlayLevel + 1; |
| 314 |
//qDebug() << "new d d->overlayLevel = " << d->overlayLevel; |
| 315 |
//qDebug() << "in PopupDropper " << this; |
| 316 |
} |
| 317 |
|
| 318 |
//NOTE: just like you have to show the overlay when adding, this function does not hide the overlay; you must do that yourself |
| 319 |
//with hide() (which will hide just one level) |
| 320 |
bool PopupDropper::subtractOverlay() |
| 321 |
{ |
| 322 |
//qDebug() << "subtractOverlay, with d pointer " << d; |
| 323 |
disconnect( this, &PopupDropper::fadeHideFinished, this, &PopupDropper::subtractOverlay ); |
| 324 |
while( !isHidden() && d->fadeHideTimer.state() == QTimeLine::Running ) |
| 325 |
{ |
| 326 |
//qDebug() << "singleshotting subtractOverlay"; |
| 327 |
QTimer::singleShot( 0, this, &PopupDropper::subtractOverlay ); |
| 328 |
return false; |
| 329 |
} |
| 330 |
//qDebug() << "in PopupDropper " << this; |
| 331 |
//qDebug() << "overlay d->overlayLevel = " << d->overlayLevel; |
| 332 |
if( d->overlayLevel == 1 ) |
| 333 |
return false; |
| 334 |
PopupDropper::Fading currFadeValue = d->fade; |
| 335 |
d->fade = PopupDropper::NoFade; |
| 336 |
d->onTop = false; |
| 337 |
PopupDropperPrivate* old_d = d; |
| 338 |
//qDebug() << "right before pop, m_viewStack.size() is " << m_viewStack.size(); |
| 339 |
d = m_viewStack.pop(); |
| 340 |
d->onTop = true; |
| 341 |
if( !old_d->submenu ) |
| 342 |
{ |
| 343 |
//qDebug() << "not submenu, deleting"; |
| 344 |
old_d->deleteLater(); |
| 345 |
} |
| 346 |
else |
| 347 |
{ |
| 348 |
for( QGraphicsItem* item : old_d->pdiItems ) |
| 349 |
old_d->scene->removeItem( item ); |
| 350 |
//qDebug() << "not deleting, submenu"; |
| 351 |
old_d->fade = currFadeValue; |
| 352 |
old_d->view->resetView(); |
| 353 |
} |
| 354 |
d->startDeleteTimer(); |
| 355 |
return true; |
| 356 |
} |
| 357 |
|
| 358 |
PopupDropperItem* PopupDropper::addSubmenu( PopupDropper** pd, const QString &text ) |
| 359 |
{ |
| 360 |
//qDebug() << "addSubmenu, this is " << this << " and passed-in PopupDropper is " << (*pd); |
| 361 |
if( !(*pd) ) |
| 362 |
{ |
| 363 |
qWarning() << "Did not pass in a valid PUD!"; |
| 364 |
return nullptr; |
| 365 |
} |
| 366 |
PopupDropperPrivate* newD = (*pd)->d; |
| 367 |
newD->submenu = true; |
| 368 |
newD->widget = d->widget; |
| 369 |
newD->setParent( this ); |
| 370 |
for( PopupDropperItem* item : newD->pdiItems ) |
| 371 |
newD->scene->removeItem( item ); |
| 372 |
newD->newSceneView( this ); |
| 373 |
initOverlay( d->widget, newD ); |
| 374 |
PopupDropperItem* pdi = new PopupDropperItem(); |
| 375 |
|
| 376 |
QAction* action = new QAction( text, this ); |
| 377 |
|
| 378 |
connect( action, &QAction::hovered, this, &PopupDropper::activateSubmenu ); |
| 379 |
pdi->setAction( action ); |
| 380 |
pdi->setSubmenuTrigger( true ); |
| 381 |
pdi->setHoverIndicatorShowStyle( PopupDropperItem::OnHover ); |
| 382 |
d->submenuMap[action] = newD; |
| 383 |
delete (*pd); |
| 384 |
(*pd) = nullptr; |
| 385 |
for( PopupDropperItem* item : newD->pdiItems ) |
| 386 |
item->setPopupDropper( this ); |
| 387 |
//qDebug() << "d->submenuMap[pda] = " << d->submenuMap[pda]; |
| 388 |
addItem( pdi ); |
| 389 |
return pdi; |
| 390 |
} |
| 391 |
|
| 392 |
void PopupDropper::activateSubmenu() |
| 393 |
{ |
| 394 |
//qDebug() << "Sender is " << QObject::sender(); |
| 395 |
if( isHidden() || d->fadeHideTimer.state() == QTimeLine::Running ) |
| 396 |
return; |
| 397 |
PopupDropperPrivate* oldd = d; |
| 398 |
addOverlay( d->submenuMap[static_cast<QAction*>(QObject::sender())] ); |
| 399 |
//qDebug() << "d->pdiItems.size() = " << d->pdiItems.size() << " for " << d; |
| 400 |
for( PopupDropperItem* item : d->pdiItems ) |
| 401 |
addItem( item, false, false ); |
| 402 |
oldd->view->deactivateHover(); |
| 403 |
show(); |
| 404 |
} |
| 405 |
|
| 406 |
bool PopupDropper::addMenu( const QMenu *menu ) |
| 407 |
{ |
| 408 |
Q_UNUSED(menu) |
| 409 |
if( !menu ) |
| 410 |
return false; |
| 411 |
|
| 412 |
if( menu->actions().isEmpty() ) |
| 413 |
return true; |
| 414 |
|
| 415 |
PopupDropperItem *pdi = nullptr; |
| 416 |
for( QAction *action : menu->actions() ) |
| 417 |
{ |
| 418 |
if( !action->menu() ) |
| 419 |
{ |
| 420 |
pdi = new PopupDropperItem(); |
| 421 |
pdi->setAction( action ); |
| 422 |
addItem( pdi ); |
| 423 |
} |
| 424 |
else |
| 425 |
{ |
| 426 |
PopupDropper *pd = new PopupDropper( nullptr ); |
| 427 |
bool success = pd->addMenu( action->menu() ); |
| 428 |
if( success ) |
| 429 |
pdi = addSubmenu( &pd, action->text() ); |
| 430 |
} |
| 431 |
pdi = nullptr; |
| 432 |
} |
| 433 |
|
| 434 |
return true; |
| 435 |
} |
| 436 |
|
| 437 |
bool PopupDropper::standalone() const |
| 438 |
{ |
| 439 |
return d->standalone; |
| 440 |
} |
| 441 |
|
| 442 |
void PopupDropper::show() |
| 443 |
{ |
| 444 |
if( !d->sharedRenderer ) |
| 445 |
{ |
| 446 |
//qDebug() << "No shared renderer set!"; |
| 447 |
return; |
| 448 |
} |
| 449 |
if( d->widget && d->widget->rect() != d->widgetRect ) |
| 450 |
{ |
| 451 |
d->widgetRect = d->widget->rect(); |
| 452 |
d->scene->setSceneRect( d->widget->rect() ); |
| 453 |
d->view->setFixedSize( d->widget->size() ); |
| 454 |
update(); |
| 455 |
} |
| 456 |
//qDebug() << "Showing PopupDropper"; |
| 457 |
d->fadeShowTimer.stop(); |
| 458 |
if( ( d->fade == PopupDropper::FadeIn || d->fade == PopupDropper::FadeInOut ) && d->fadeInTime > 0 ) |
| 459 |
{ |
| 460 |
//qDebug() << "Animating!"; |
| 461 |
d->fadeShowTimer.setDuration( d->fadeInTime ); |
| 462 |
d->fadeShowTimer.setCurrentTime( 0 ); |
| 463 |
d->fadeShowTimer.setEasingCurve( QEasingCurve::OutCurve ); |
| 464 |
QColor color = d->windowColor; |
| 465 |
color.setAlpha( 0 ); |
| 466 |
setPalette( color ); |
| 467 |
for( PopupDropperItem* pdi : d->pdiItems ) |
| 468 |
pdi->setSubitemOpacity( 0.0 ); |
| 469 |
d->fadeShowTimer.start(); |
| 470 |
//qDebug() << "Timer started"; |
| 471 |
} |
| 472 |
d->view->show(); |
| 473 |
} |
| 474 |
|
| 475 |
void PopupDropper::showAllOverlays() |
| 476 |
{ |
| 477 |
show(); |
| 478 |
for( int i = m_viewStack.size() - 1; i >= 0; --i ) |
| 479 |
{ |
| 480 |
PopupDropperPrivate* pdi = m_viewStack.at( i ); |
| 481 |
if( pdi != d ) |
| 482 |
d->view->show(); |
| 483 |
} |
| 484 |
} |
| 485 |
|
| 486 |
//returns immediately! |
| 487 |
void PopupDropper::hide() |
| 488 |
{ |
| 489 |
//qDebug() << "PopupDropper::hide entered, d pointer is = " << d; |
| 490 |
|
| 491 |
//if hide is called and the view is already hidden, it's likely spurious |
| 492 |
if( isHidden() ) |
| 493 |
{ |
| 494 |
//qDebug() << "ishidden, returning"; |
| 495 |
return; |
| 496 |
} |
| 497 |
|
| 498 |
//queuedHide is to make sure that fadeShowTimerFinished executes before this next hide() |
| 499 |
if( d->fadeShowTimer.state() == QTimeLine::Running ) |
| 500 |
{ |
| 501 |
//qDebug() << "show timer running, queueing hide"; |
| 502 |
d->fadeShowTimer.stop(); |
| 503 |
d->queuedHide = true; |
| 504 |
QTimer::singleShot( 0, d, &PopupDropperPrivate::fadeShowTimerFinished ); |
| 505 |
QTimer::singleShot( 0, this, &PopupDropper::hide ); |
| 506 |
return; |
| 507 |
} |
| 508 |
|
| 509 |
//queuedHide will be set to false from fadeShowTimerFinished...so if this came up first, |
| 510 |
//then wait |
| 511 |
if( d->fadeHideTimer.state() == QTimeLine::Running || d->queuedHide ) |
| 512 |
{ |
| 513 |
//qDebug() << "hide timer running or queued hide"; |
| 514 |
QTimer::singleShot( 0, this, &PopupDropper::hide ); |
| 515 |
return; |
| 516 |
} |
| 517 |
|
| 518 |
if( ( d->fade == PopupDropper::FadeOut || d->fade == PopupDropper::FadeInOut ) && d->fadeOutTime > 0 ) |
| 519 |
{ |
| 520 |
//qDebug() << "Starting fade out"; |
| 521 |
d->fadeHideTimer.setDuration( d->fadeOutTime ); |
| 522 |
d->fadeHideTimer.setEasingCurve( QEasingCurve::Linear ); |
| 523 |
d->fadeHideTimer.start(); |
| 524 |
//qDebug() << "Timer started"; |
| 525 |
return; |
| 526 |
} |
| 527 |
else //time is zero, or no fade |
| 528 |
{ |
| 529 |
//qDebug() << "time is zero, or no fade"; |
| 530 |
QTimer::singleShot( 0, d, &PopupDropperPrivate::fadeHideTimerFinished ); |
| 531 |
return; |
| 532 |
} |
| 533 |
} |
| 534 |
|
| 535 |
void PopupDropper::hideAllOverlays() |
| 536 |
{ |
| 537 |
//qDebug() << "Entered hideAllOverlays"; |
| 538 |
connect( this, &PopupDropper::fadeHideFinished, this, &PopupDropper::slotHideAllOverlays ); |
| 539 |
hide(); |
| 540 |
//qDebug() << "Leaving hideAllOverlays"; |
| 541 |
} |
| 542 |
|
| 543 |
void PopupDropper::slotHideAllOverlays() |
| 544 |
{ |
| 545 |
//qDebug() << "Entered slotHideAllOverlays()"; |
| 546 |
disconnect( this, &PopupDropper::fadeHideFinished, this, &PopupDropper::slotHideAllOverlays ); |
| 547 |
//qDebug() << "m_viewStack.size() = " << m_viewStack.size(); |
| 548 |
for( int i = m_viewStack.size() - 1; i >= 0; --i ) |
| 549 |
{ |
| 550 |
PopupDropperPrivate* pdp = m_viewStack.at( i ); |
| 551 |
//qDebug() << "checking pdp = " << (QObject*)pdp << ", d is " << (QObject*)d; |
| 552 |
if( pdp != d ) |
| 553 |
pdp->view->hide(); |
| 554 |
} |
| 555 |
//qDebug() << "Leaving slotHideAllOverlays"; |
| 556 |
} |
| 557 |
|
| 558 |
void PopupDropper::update() |
| 559 |
{ |
| 560 |
d->reposItems(); |
| 561 |
d->view->update(); |
| 562 |
} |
| 563 |
|
| 564 |
void PopupDropper::updateAllOverlays() |
| 565 |
{ |
| 566 |
for( int i = m_viewStack.size() - 1; i >= 0; --i ) |
| 567 |
{ |
| 568 |
PopupDropperPrivate* pdp = m_viewStack.at( i ); |
| 569 |
pdp->view->update(); |
| 570 |
} |
| 571 |
d->view->update(); |
| 572 |
} |
| 573 |
|
| 574 |
bool PopupDropper::isHidden() const |
| 575 |
{ |
| 576 |
return d->view->isHidden(); |
| 577 |
} |
| 578 |
|
| 579 |
void PopupDropper::clear() |
| 580 |
{ |
| 581 |
while( !isHidden() && d->fadeHideTimer.state() == QTimeLine::Running ) |
| 582 |
{ |
| 583 |
QTimer::singleShot(0, this, &PopupDropper::clear ); |
| 584 |
return; |
| 585 |
} |
| 586 |
//qDebug() << "Clear happening!"; |
| 587 |
// disconnect( this, 0, this, &PopupDropper::clear ); Unused at this point. |
| 588 |
do |
| 589 |
{ |
| 590 |
for( QGraphicsItem* item : d->allItems ) |
| 591 |
{ |
| 592 |
if( dynamic_cast<PopupDropperItem*>(item) ) |
| 593 |
{ |
| 594 |
if( dynamic_cast<PopupDropperItem*>(item)->isSubmenuTrigger() ) |
| 595 |
{ |
| 596 |
//qDebug() << "Disconnecting action"; |
| 597 |
disconnect( dynamic_cast<PopupDropperItem*>(item)->action(), &QAction::hovered, this, &PopupDropper::activateSubmenu ); |
| 598 |
} |
| 599 |
dynamic_cast<PopupDropperItem*>(item)->deleteLater(); |
| 600 |
} |
| 601 |
else |
| 602 |
delete item; |
| 603 |
} |
| 604 |
d->pdiItems.clear(); |
| 605 |
d->allItems.clear(); |
| 606 |
//qDebug() << "Size of pdiItems is now " << d->pdiItems.size(); |
| 607 |
//qDebug() << "Size of allItems is now " << d->allItems.size(); |
| 608 |
d->view->hide(); |
| 609 |
d->view->resetView(); |
| 610 |
} while ( subtractOverlay() ); |
| 611 |
} |
| 612 |
|
| 613 |
bool PopupDropper::isEmpty( bool allItems ) const |
| 614 |
{ |
| 615 |
if( allItems ) |
| 616 |
return d->allItems.empty(); |
| 617 |
else |
| 618 |
return d->pdiItems.empty(); |
| 619 |
} |
| 620 |
|
| 621 |
bool PopupDropper::quitOnDragLeave() const |
| 622 |
{ |
| 623 |
return d->quitOnDragLeave; |
| 624 |
} |
| 625 |
|
| 626 |
void PopupDropper::setQuitOnDragLeave( bool quit ) |
| 627 |
{ |
| 628 |
d->quitOnDragLeave = quit; |
| 629 |
} |
| 630 |
|
| 631 |
int PopupDropper::fadeInTime() const |
| 632 |
{ |
| 633 |
return d->fadeInTime; |
| 634 |
} |
| 635 |
|
| 636 |
void PopupDropper::setFadeInTime( const int msecs ) |
| 637 |
{ |
| 638 |
d->fadeInTime = msecs; |
| 639 |
} |
| 640 |
|
| 641 |
int PopupDropper::fadeOutTime() const |
| 642 |
{ |
| 643 |
return d->fadeOutTime; |
| 644 |
} |
| 645 |
|
| 646 |
void PopupDropper::setFadeOutTime( const int msecs ) |
| 647 |
{ |
| 648 |
d->fadeOutTime = msecs; |
| 649 |
} |
| 650 |
|
| 651 |
PopupDropper::Fading PopupDropper::fading() const |
| 652 |
{ |
| 653 |
return d->fade; |
| 654 |
} |
| 655 |
|
| 656 |
void PopupDropper::setFading( PopupDropper::Fading fade ) |
| 657 |
{ |
| 658 |
d->fade = fade; |
| 659 |
} |
| 660 |
|
| 661 |
const QTimeLine* PopupDropper::fadeHideTimer() const |
| 662 |
{ |
| 663 |
return &d->fadeHideTimer; |
| 664 |
} |
| 665 |
|
| 666 |
const QTimeLine* PopupDropper::fadeShowTimer() const |
| 667 |
{ |
| 668 |
return &d->fadeShowTimer; |
| 669 |
} |
| 670 |
|
| 671 |
int PopupDropper::deleteTimeout() const |
| 672 |
{ |
| 673 |
return d->deleteTimeout; |
| 674 |
} |
| 675 |
|
| 676 |
void PopupDropper::setDeleteTimeout( int msecs ) |
| 677 |
{ |
| 678 |
d->deleteTimeout = msecs; |
| 679 |
} |
| 680 |
|
| 681 |
QColor PopupDropper::windowColor() const |
| 682 |
{ |
| 683 |
return d->windowColor; |
| 684 |
} |
| 685 |
|
| 686 |
void PopupDropper::setWindowColor( const QColor &window ) |
| 687 |
{ |
| 688 |
d->windowColor = window; |
| 689 |
setPalette( window ); |
| 690 |
} |
| 691 |
|
| 692 |
QBrush PopupDropper::windowBackgroundBrush() const |
| 693 |
{ |
| 694 |
return d->windowBackgroundBrush; |
| 695 |
} |
| 696 |
|
| 697 |
void PopupDropper::setWindowBackgroundBrush( const QBrush &window ) |
| 698 |
{ |
| 699 |
d->windowBackgroundBrush = window; |
| 700 |
d->view->setBackgroundBrush( window ); |
| 701 |
} |
| 702 |
|
| 703 |
QColor PopupDropper::baseTextColor() const |
| 704 |
{ |
| 705 |
return d->baseTextColor; |
| 706 |
} |
| 707 |
|
| 708 |
void PopupDropper::setBaseTextColor( const QColor &text ) |
| 709 |
{ |
| 710 |
d->baseTextColor = text; |
| 711 |
for( PopupDropperItem* item : d->pdiItems ) |
| 712 |
item->setBaseTextColor( text ); |
| 713 |
} |
| 714 |
|
| 715 |
QColor PopupDropper::hoveredTextColor() const |
| 716 |
{ |
| 717 |
return d->hoveredTextColor; |
| 718 |
} |
| 719 |
|
| 720 |
void PopupDropper::setHoveredTextColor( const QColor &text ) |
| 721 |
{ |
| 722 |
d->hoveredTextColor = text; |
| 723 |
for( PopupDropperItem* item : d->pdiItems ) |
| 724 |
item->setHoveredTextColor( text ); |
| 725 |
} |
| 726 |
|
| 727 |
QPen PopupDropper::hoveredBorderPen() const |
| 728 |
{ |
| 729 |
return d->hoveredBorderPen; |
| 730 |
} |
| 731 |
|
| 732 |
void PopupDropper::setHoveredBorderPen( const QPen &border ) |
| 733 |
{ |
| 734 |
d->hoveredBorderPen = border; |
| 735 |
for( PopupDropperItem* item : d->pdiItems ) |
| 736 |
item->setHoveredBorderPen( border ); |
| 737 |
} |
| 738 |
|
| 739 |
QBrush PopupDropper::hoveredFillBrush() const |
| 740 |
{ |
| 741 |
return d->hoveredFillBrush; |
| 742 |
} |
| 743 |
|
| 744 |
void PopupDropper::setHoveredFillBrush( const QBrush &fill ) |
| 745 |
{ |
| 746 |
d->hoveredFillBrush = fill; |
| 747 |
for( PopupDropperItem* item : d->pdiItems ) |
| 748 |
item->setHoveredFillBrush( fill ); |
| 749 |
} |
| 750 |
|
| 751 |
void PopupDropper::setColors( const QColor &window, const QColor &baseText, const QColor &hoveredText, const QColor &hoveredBorder, const QColor &hoveredFill ) |
| 752 |
{ |
| 753 |
d->windowColor = window; |
| 754 |
d->baseTextColor = baseText; |
| 755 |
d->hoveredTextColor = hoveredText; |
| 756 |
d->hoveredBorderPen.setColor( hoveredBorder ); |
| 757 |
d->hoveredFillBrush.setColor( hoveredFill ); |
| 758 |
setPalette( window, baseText, hoveredText, hoveredBorder, hoveredFill ); |
| 759 |
} |
| 760 |
|
| 761 |
void PopupDropper::setPalette( const QColor &window ) |
| 762 |
{ |
| 763 |
QPalette p = d->view->palette(); |
| 764 |
p.setColor( QPalette::Window, window ); |
| 765 |
d->view->setPalette( p ); |
| 766 |
updateAllOverlays(); |
| 767 |
} |
| 768 |
|
| 769 |
void PopupDropper::setPalette( const QColor &window, const QColor &baseText, const QColor &hoveredText, const QColor &hoveredBorder, const QColor &hoveredFill ) |
| 770 |
{ |
| 771 |
QPalette p = d->view->palette(); |
| 772 |
p.setColor( QPalette::Window, window ); |
| 773 |
d->view->setPalette( p ); |
| 774 |
QPen pen; |
| 775 |
QBrush brush; |
| 776 |
for( PopupDropperItem* item : d->pdiItems ) |
| 777 |
{ |
| 778 |
item->setBaseTextColor( baseText ); |
| 779 |
item->setHoveredTextColor( hoveredText ); |
| 780 |
pen = item->hoveredBorderPen(); |
| 781 |
pen.setColor( hoveredBorder ); |
| 782 |
item->setHoveredBorderPen( pen ); |
| 783 |
brush = item->hoveredFillBrush(); |
| 784 |
brush.setColor( hoveredFill ); |
| 785 |
item->setHoveredFillBrush( brush ); |
| 786 |
} |
| 787 |
updateAllOverlays(); |
| 788 |
} |
| 789 |
|
| 790 |
QString PopupDropper::windowTitle() const |
| 791 |
{ |
| 792 |
return d->view->windowTitle(); |
| 793 |
} |
| 794 |
|
| 795 |
void PopupDropper::setWindowTitle( const QString &title ) |
| 796 |
{ |
| 797 |
d->view->setWindowTitle( title ); |
| 798 |
d->view->update(); |
| 799 |
} |
| 800 |
|
| 801 |
QString PopupDropper::svgFile() const |
| 802 |
{ |
| 803 |
return d->file; |
| 804 |
} |
| 805 |
|
| 806 |
void PopupDropper::setSvgFile( const QString &file ) |
| 807 |
{ |
| 808 |
if( d->sharedRenderer ) |
| 809 |
{ |
| 810 |
if( !d->sharedRenderer->load( file ) ) |
| 811 |
qWarning() << "Could not load SVG file " << file; |
| 812 |
else |
| 813 |
{ |
| 814 |
d->file = file; |
| 815 |
//qDebug() << "Loaded SVG file!"; |
| 816 |
} |
| 817 |
} |
| 818 |
else |
| 819 |
qWarning() << "No shared renderer!"; |
| 820 |
} |
| 821 |
|
| 822 |
QSvgRenderer* PopupDropper::svgRenderer() |
| 823 |
{ |
| 824 |
return d->sharedRenderer; |
| 825 |
} |
| 826 |
|
| 827 |
void PopupDropper::setSvgRenderer( QSvgRenderer *renderer ) |
| 828 |
{ |
| 829 |
d->sharedRenderer = renderer; |
| 830 |
} |
| 831 |
|
| 832 |
int PopupDropper::horizontalOffset() const |
| 833 |
{ |
| 834 |
return d->horizontalOffset; |
| 835 |
} |
| 836 |
|
| 837 |
void PopupDropper::setHorizontalOffset( int pixels ) |
| 838 |
{ |
| 839 |
d->horizontalOffset = pixels; |
| 840 |
} |
| 841 |
|
| 842 |
const QSize PopupDropper::viewSize() const |
| 843 |
{ |
| 844 |
if( d && d->view ) |
| 845 |
return d->view->size(); |
| 846 |
else |
| 847 |
return QSize( 0, 0 ); |
| 848 |
} |
| 849 |
|
| 850 |
void PopupDropper::addItem( PopupDropperItem *item, bool useSharedRenderer ) |
| 851 |
{ |
| 852 |
addItem( item, useSharedRenderer, true ); |
| 853 |
} |
| 854 |
|
| 855 |
void PopupDropper::addItem( PopupDropperItem *item, bool useSharedRenderer, bool appendToList ) |
| 856 |
{ |
| 857 |
//qDebug() << "adding item"; |
| 858 |
//FIXME: Make separators do something graphical instead of just ignoring them |
| 859 |
PopupDropperItem *pItem = static_cast<PopupDropperItem*>( item ); |
| 860 |
if( pItem->isSeparator() ) |
| 861 |
return; |
| 862 |
if( useSharedRenderer ) |
| 863 |
pItem->setSharedRenderer( d->sharedRenderer ); |
| 864 |
//qDebug() << "Checking appendToList"; |
| 865 |
if( appendToList ) |
| 866 |
{ |
| 867 |
d->pdiItems.append( pItem ); |
| 868 |
d->allItems.append( pItem ); |
| 869 |
//qDebug() << "pdiItems list is now size " << d->pdiItems.size() << " for " << d; |
| 870 |
//qDebug() << "allItems list is now size " << d->allItems.size() << " for " << d; |
| 871 |
} |
| 872 |
if( !pItem->textItem() ) |
| 873 |
{ |
| 874 |
QGraphicsTextItem *textItem = new QGraphicsTextItem( pItem->text(), pItem ); |
| 875 |
pItem->setTextItem( textItem ); |
| 876 |
if( !pItem->customBaseTextColor() || !pItem->baseTextColor().isValid() ) |
| 877 |
{ |
| 878 |
pItem->setBaseTextColor( d->baseTextColor ); |
| 879 |
//qDebug() << "Using PD's base text color"; |
| 880 |
} |
| 881 |
else |
| 882 |
{ |
| 883 |
//qDebug() << "Using the item's base text color"; |
| 884 |
pItem->textItem()->setDefaultTextColor( pItem->baseTextColor() ); |
| 885 |
} |
| 886 |
if( !pItem->customHoveredTextColor() ) |
| 887 |
pItem->setHoveredTextColor( d->hoveredTextColor ); |
| 888 |
} |
| 889 |
if( !pItem->borderRectItem() ) |
| 890 |
{ |
| 891 |
QGraphicsRectItem *borderRectItem = new QGraphicsRectItem( pItem ); |
| 892 |
borderRectItem->setZValue( -5 ); |
| 893 |
pItem->setBorderRectItem( borderRectItem ); |
| 894 |
if( !pItem->customHoveredBorderPen() ) |
| 895 |
pItem->setHoveredBorderPen( d->hoveredBorderPen ); |
| 896 |
if( !pItem->customHoveredFillBrush() ) |
| 897 |
pItem->setHoveredFillBrush( d->hoveredFillBrush ); |
| 898 |
} |
| 899 |
d->reposItems(); |
| 900 |
pItem->setPopupDropper( this ); |
| 901 |
d->scene->addItem( pItem ); |
| 902 |
} |
| 903 |
|
| 904 |
QList<PopupDropperItem*> PopupDropper::items() const |
| 905 |
{ |
| 906 |
QList<PopupDropperItem*> list; |
| 907 |
for( PopupDropperItem* item : d->pdiItems ) |
| 908 |
list.append( item ); |
| 909 |
|
| 910 |
return list; |
| 911 |
} |
| 912 |
|
| 913 |
//Won't currently work for > 1 level of submenu! |
| 914 |
//TODO: Figure out a better way. (Does anything else work > 1 level?) |
| 915 |
QList<PopupDropperItem*> PopupDropper::submenuItems( const PopupDropperItem *item ) const |
| 916 |
{ |
| 917 |
QList<PopupDropperItem*> list; |
| 918 |
if( !item || !item->isSubmenuTrigger() || !d->submenuMap.contains( item->action() ) ) |
| 919 |
return list; |
| 920 |
|
| 921 |
PopupDropperPrivate *pdp = d->submenuMap[item->action()]; |
| 922 |
list.reserve(pdp->pdiItems.count()); |
| 923 |
for( PopupDropperItem* pdi : d->pdiItems ) |
| 924 |
list.append( pdi ); |
| 925 |
|
| 926 |
return list; |
| 927 |
} |
| 928 |
|
| 929 |
//Goes through and calls the callback on all items, including submenuItems |
| 930 |
//which can be adjusted differently by checking isSubmenuTrigger() |
| 931 |
void PopupDropper::forEachItem( void callback(void*) ) |
| 932 |
{ |
| 933 |
forEachItemPrivate( d, callback ); |
| 934 |
} |
| 935 |
|
| 936 |
void PopupDropper::forEachItemPrivate( PopupDropperPrivate *pdp, void callback(void* item) ) |
| 937 |
{ |
| 938 |
for( PopupDropperItem* item : d->pdiItems ) |
| 939 |
callback( item ); |
| 940 |
for( QAction *action : pdp->submenuMap.keys() ) |
| 941 |
forEachItemPrivate( pdp->submenuMap[action], callback ); |
| 942 |
} |
| 943 |
|
| 944 |
void PopupDropper::addSeparator( PopupDropperItem* separator ) |
| 945 |
{ |
| 946 |
|
| 947 |
if( !separator ) |
| 948 |
{ |
| 949 |
//qDebug() << "Action is not a separator!"; |
| 950 |
return; |
| 951 |
} |
| 952 |
|
| 953 |
separator->setSeparator( true ); |
| 954 |
|
| 955 |
if( separator->separatorStyle() == PopupDropperItem::TextSeparator ) |
| 956 |
{ |
| 957 |
//qDebug() << "Separator style is text"; |
| 958 |
addItem( separator ); |
| 959 |
} |
| 960 |
|
| 961 |
//qDebug() << "Separator style is line"; |
| 962 |
QPen linePen; |
| 963 |
if( separator && separator->hasLineSeparatorPen() ) |
| 964 |
linePen = separator->lineSeparatorPen(); |
| 965 |
else |
| 966 |
{ |
| 967 |
linePen.setWidth( 2 ); |
| 968 |
linePen.setCapStyle( Qt::RoundCap ); |
| 969 |
linePen.setStyle( Qt::DotLine ); |
| 970 |
linePen.setColor( QColor( 255, 255, 255 ) ); |
| 971 |
} |
| 972 |
|
| 973 |
//qDebug() << "scene width = " << d->scene->width() << ", horizontalOffset = " << d->horizontalOffset; |
| 974 |
//qDebug() << "right side = " << qreal(d->scene->width() - d->horizontalOffset); |
| 975 |
QGraphicsLineItem* lineItem = new QGraphicsLineItem( 0, 0, 0, 0 ); |
| 976 |
d->allItems.append( lineItem ); |
| 977 |
lineItem->setPen( linePen ); |
| 978 |
d->reposItems(); |
| 979 |
d->scene->addItem( lineItem ); |
| 980 |
} |