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
|
/****************************************************************************
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 "pTabbedWorkspaceCornerButton.h"
#include <QPaintEvent>
#include <QMouseEvent>
#include <QStyleOptionToolButton>
#include <QToolBar>
#include <QMenu>
#include <QPainter>
#include <QApplication>
pTabbedWorkspaceCornerButton::pTabbedWorkspaceCornerButton( QWidget* p, QBoxLayout::Direction d )
: QToolButton( p ), mMenuDown( false )
{
/*
QBoxLayout::LeftToRight
QBoxLayout::RightToLeft
QBoxLayout::TopToBottom
QBoxLayout::BottomToTop
*/
setDirection( d );
}
void pTabbedWorkspaceCornerButton::paintEvent( QPaintEvent* )
{
// calcul angle rotation
QSize s = QToolButton::sizeHint();
int r = 0;
QPoint p = QPoint();
switch ( mDirection )
{
case QBoxLayout::TopToBottom:
r = 90;
p = QPoint( 0, -s.height() );
break;
case QBoxLayout::BottomToTop:
r = -90;
p = QPoint( -s.width(), 0 );
break;
default:
break;
}
// do rotated button
QPixmap pixmap( s );
pixmap.fill( QColor( 0, 0, 0, 0 ) );
// get style options
QStyleOptionToolButton o;
setStyleOption( &o );
// force to do horizontal paint
o.rect.setSize( s );
// fix backport bugs :|
switch ( cursorArea() )
{
case pTabbedWorkspaceCornerButton::caArrow:
break;
case pTabbedWorkspaceCornerButton::caButton:
break;
case pTabbedWorkspaceCornerButton::caArrowClicked:
break;
case pTabbedWorkspaceCornerButton::caButtonClicked:
o.activeSubControls |= QStyle::SC_ToolButton;
if ( popupMode() == QToolButton::MenuButtonPopup )
{
o.state |= QStyle::State_MouseOver;
o.activeSubControls |= QStyle::SC_ToolButtonMenu;
}
break;
case pTabbedWorkspaceCornerButton::caNone:
default:
break;
}
// draw button to pixmap
QPainter pixpainter( &pixmap );
style()->drawComplexControl( QStyle::CC_ToolButton, &o, &pixpainter, this );
// draw pixmap on button
QPainter painter( this );
painter.rotate( r );
painter.drawPixmap( p, pixmap );
}
void pTabbedWorkspaceCornerButton::mousePressEvent( QMouseEvent* e )
{
switch ( cursorArea( e->pos() ) )
{
case pTabbedWorkspaceCornerButton::caArrowClicked:
mMenuDown = true;
showMenu();
mMenuDown = false;
break;
case pTabbedWorkspaceCornerButton::caButtonClicked:
break;
case pTabbedWorkspaceCornerButton::caNone:
break;
default:
QAbstractButton::mousePressEvent( e );
break;
}
// update button
update();
}
void pTabbedWorkspaceCornerButton::mouseReleaseEvent( QMouseEvent* e )
{
mMenuDown = false;
switch ( cursorArea( e->pos() ) )
{
case pTabbedWorkspaceCornerButton::caArrow:
break;
case pTabbedWorkspaceCornerButton::caButton:
click();
break;
case pTabbedWorkspaceCornerButton::caNone:
break;
default:
QAbstractButton::mouseReleaseEvent( e );
break;
}
// update button
update();
}
pTabbedWorkspaceCornerButton::CursorArea pTabbedWorkspaceCornerButton::cursorArea( const QPoint& pos ) const
{
// cursor pos
const QPoint p = pos.isNull() ? mapFromGlobal( QCursor::pos() ) : pos;
// if not contain is button return none
if ( !hitButton( p ) )
return pTabbedWorkspaceCornerButton::caNone;
// is arrow type
bool a = popupMode() == QToolButton::MenuButtonPopup;
// is mouse pressed ?!
bool m = QApplication::mouseButtons() & Qt::LeftButton;
// check if we are a arrow button
if ( a )
{
// get bounding rectangle
QRect r = rect();
// get style options
QStyleOptionToolButton opt;
setStyleOption( &opt );
// force to do horizontal calcul
opt.rect.setSize( QToolButton::sizeHint() );
// get arraow bounding rectangle
QSize s = style()->subControlRect( QStyle::CC_ToolButton, &opt, QStyle::SC_ToolButtonMenu, this ).size();
switch ( mDirection )
{
case QBoxLayout::BottomToTop:
s.transpose();
break;
case QBoxLayout::TopToBottom:
s.transpose();
r.setY( r.height() -s.height() );
break;
default:
r.setX( r.width() -s.width() );
break;
}
// get valid bounding rectangle size
r.setSize( s );
// in arrow bounding rect
if ( r.isValid() && r.contains( p ) )
return m ? pTabbedWorkspaceCornerButton::caArrowClicked : pTabbedWorkspaceCornerButton::caArrow;
}
// in button
return m ? pTabbedWorkspaceCornerButton::caButtonClicked : pTabbedWorkspaceCornerButton::caButton;
}
QMenu* pTabbedWorkspaceCornerButton::hasMenu() const
{
QMenu* m = menu();
if ( !m && defaultAction() )
m = defaultAction()->menu();
return m;
}
bool pTabbedWorkspaceCornerButton::menuButtonDown() const
{
#ifndef QT_NO_MENU
return hasMenu() && mMenuDown;
#else
return false;
#endif
}
void pTabbedWorkspaceCornerButton::setStyleOption( QStyleOptionToolButton* option ) const
{
if ( !option )
return;
#if QT_VERSION >= 0x040300
initStyleOption( option );
#else
// backported from Qt 4.3
option->initFrom( this );
bool forceNoText = false;
#ifndef QT_NO_TOOLBAR
if ( parentWidget() )
{
#ifdef QT3_SUPPORT
if ( parentWidget()->inherits( "Q3ToolBar" ) )
{
int iconSize = style()->pixelMetric( QStyle::PM_ToolBarIconSize, option, this );
option->iconSize = icon().actualSize( QSize( iconSize, iconSize ) );
forceNoText = toolButtonStyle() == Qt::ToolButtonIconOnly;
}
else
#endif
if ( QToolBar* toolBar = qobject_cast<QToolBar*>( parentWidget() ) )
{
option->iconSize = toolBar->iconSize();
}
else
{
option->iconSize = iconSize();
}
}
#endif // QT_NO_TOOLBAR
if ( !forceNoText )
option->text = text();
option->icon = icon();
option->arrowType = arrowType();
if ( isDown() )
option->state |= QStyle::State_Sunken;
if ( isChecked() )
option->state |= QStyle::State_On;
if ( autoRaise() )
option->state |= QStyle::State_AutoRaise;
if ( !isChecked() && !isDown() )
option->state |= QStyle::State_Raised;
option->subControls = QStyle::SC_ToolButton;
option->activeSubControls = QStyle::SC_None;
//if ( isDown() && !menuButtonDown() )
//option->activeSubControls |= QStyle::SC_ToolButton;
option->features = QStyleOptionToolButton::None;
if ( popupMode() == QToolButton::MenuButtonPopup )
{
option->subControls |= QStyle::SC_ToolButtonMenu;
#if QT_VERSION >= 0x040300
option->features |= QStyleOptionToolButton::MenuButtonPopup;
#endif
if ( menuButtonDown() || isDown() )
{
option->state |= QStyle::State_MouseOver;
option->activeSubControls |= QStyle::SC_ToolButtonMenu;
}
}
else
{
if ( menuButtonDown() )
option->state |= QStyle::State_Sunken;
}
if ( arrowType() != Qt::NoArrow )
option->features |= QStyleOptionToolButton::Arrow;
if ( popupMode() == QToolButton::DelayedPopup )
option->features |= QStyleOptionToolButton::PopupDelay;
#ifndef QT_NO_MENU
if ( hasMenu() )
#if QT_VERSION >= 0x040300
option->features |= QStyleOptionToolButton::HasMenu;
#else
option->features |= QStyleOptionToolButton::Menu;
#endif
#endif
option->toolButtonStyle = toolButtonStyle();
if ( icon().isNull() && arrowType() == Qt::NoArrow && !forceNoText)
{
if ( !text().isEmpty() )
option->toolButtonStyle = Qt::ToolButtonTextOnly;
else if ( option->toolButtonStyle != Qt::ToolButtonTextOnly )
option->toolButtonStyle = Qt::ToolButtonIconOnly;
}
else
{
if ( text().isEmpty() && option->toolButtonStyle != Qt::ToolButtonIconOnly )
option->toolButtonStyle = Qt::ToolButtonIconOnly;
}
option->pos = pos();
option->font = font();
#endif // QT_VERSION >= 0x040300
}
QSize pTabbedWorkspaceCornerButton::sizeHint() const
{
//get default size
QSize s = QToolButton::sizeHint();
// calcul new size hint
switch ( mDirection )
{
case QBoxLayout::LeftToRight:
case QBoxLayout::RightToLeft:
break;
case QBoxLayout::TopToBottom:
case QBoxLayout::BottomToTop:
s.transpose();
break;
}
// return new size hint;
return s;
}
QBoxLayout::Direction pTabbedWorkspaceCornerButton::direction() const
{
return mDirection;
}
void pTabbedWorkspaceCornerButton::setDirection( QBoxLayout::Direction d )
{
if ( mDirection == d )
return;
mDirection = d;
update();
}
|