diff --git a/kdevplatform/sublime/idealbuttonbarwidget.cpp b/kdevplatform/sublime/idealbuttonbarwidget.cpp index 918d1a36a3..d76597411e 100644 --- a/kdevplatform/sublime/idealbuttonbarwidget.cpp +++ b/kdevplatform/sublime/idealbuttonbarwidget.cpp @@ -1,363 +1,360 @@ /* Copyright 2007 Roberto Raggi Copyright 2007 Hamish Rodda Copyright 2011 Alexander Dymo Permission to use, copy, modify, distribute, and sell this software and its documentation for any purpose is hereby granted without fee, provided that the above copyright notice appear in all copies and that both that copyright notice and this permission notice appear in supporting documentation. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE KDEVELOP TEAM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include "idealbuttonbarwidget.h" #include "mainwindow.h" #include "idealdockwidget.h" #include "ideallayout.h" #include "idealtoolbutton.h" #include "document.h" #include "view.h" #include #include #include #include #include using namespace Sublime; class ToolViewAction : public QAction { Q_OBJECT public: ToolViewAction(IdealDockWidget *dock, QObject* parent) : QAction(parent), m_dock(dock) { setCheckable(true); const QString title = dock->view()->document()->title(); setIcon(dock->windowIcon()); setToolTip(i18n("Toggle '%1' tool view.", title)); setText(title); //restore tool view shortcut config KConfigGroup config = KSharedConfig::openConfig()->group("UI"); QStringList shortcutStrings = config.readEntry(QStringLiteral("Shortcut for %1").arg(title), QStringList()); setShortcuts({ QKeySequence::fromString(shortcutStrings.value(0)), QKeySequence::fromString(shortcutStrings.value(1)) }); dock->setWindowTitle(title); dock->view()->widget()->installEventFilter(this); refreshText(); } IdealDockWidget *dockWidget() const { Q_ASSERT(m_dock); return m_dock; } IdealToolButton* button() { return m_button; } void setButton(IdealToolButton* button) { m_button = button; refreshText(); } QString id() { return m_dock->view()->document()->documentSpecifier(); } private: bool eventFilter(QObject * watched, QEvent * event) override { if (watched == m_dock->view()->widget() && event->type() == QEvent::EnabledChange) { refreshText(); } return QObject::eventFilter(watched, event); } void refreshText() { const auto widget = m_dock->view()->widget(); const QString title = m_dock->view()->document()->title(); setText(widget->isEnabled() ? title : QStringLiteral("(%1)").arg(title)); } QPointer m_dock; QPointer m_button; }; IdealButtonBarWidget::IdealButtonBarWidget(Qt::DockWidgetArea area, IdealController *controller, Sublime::MainWindow *parent) : QWidget(parent) , m_area(area) , m_controller(controller) , m_corner(nullptr) , m_showState(false) + , m_buttonsLayout(nullptr) { setContextMenuPolicy(Qt::CustomContextMenu); setToolTip(i18nc("@info:tooltip", "Right click to add new tool views.")); if (area == Qt::BottomDockWidgetArea) { - QBoxLayout *statusLayout = new QBoxLayout(QBoxLayout::RightToLeft, this); + QBoxLayout *statusLayout = new QBoxLayout(QBoxLayout::LeftToRight, this); statusLayout->setMargin(0); - IdealButtonBarLayout *l = new IdealButtonBarLayout(orientation()); - statusLayout->addLayout(l); + m_buttonsLayout = new IdealButtonBarLayout(orientation()); + statusLayout->addLayout(m_buttonsLayout); + + statusLayout->addStretch(1); m_corner = new QWidget(this); QBoxLayout *cornerLayout = new QBoxLayout(QBoxLayout::LeftToRight, m_corner); cornerLayout->setMargin(0); cornerLayout->setSpacing(0); statusLayout->addWidget(m_corner); - statusLayout->addStretch(1); } else - (void) new IdealButtonBarLayout(orientation(), this); + m_buttonsLayout = new IdealButtonBarLayout(orientation(), this); } QAction* IdealButtonBarWidget::addWidget(IdealDockWidget *dock, Area *area, View *view) { if (m_area == Qt::BottomDockWidgetArea || m_area == Qt::TopDockWidgetArea) dock->setFeatures( dock->features() | IdealDockWidget::DockWidgetVerticalTitleBar ); dock->setArea(area); dock->setView(view); dock->setDockWidgetArea(m_area); auto action = new ToolViewAction(dock, this); addAction(action); return action; } QWidget* IdealButtonBarWidget::corner() const { return m_corner; } void IdealButtonBarWidget::addAction(QAction* qaction) { QWidget::addAction(qaction); auto action = dynamic_cast(qaction); if (!action || action->button()) { return; } bool wasEmpty = isEmpty(); IdealToolButton *button = new IdealToolButton(m_area); //apol: here we set the usual width of a button for the vertical toolbars as the minimumWidth //this is done because otherwise when we remove all the buttons and re-add new ones we get all //the screen flickering. This is solved by not defaulting to a smaller width when it's empty int w = button->sizeHint().width(); if (orientation() == Qt::Vertical && w > minimumWidth()) { setMinimumWidth(w); } action->setButton(button); button->setDefaultAction(action); Q_ASSERT(action->dockWidget()); connect(action, &QAction::toggled, this, static_cast(&IdealButtonBarWidget::showWidget)); connect(button, &IdealToolButton::customContextMenuRequested, action->dockWidget(), &IdealDockWidget::contextMenuRequested); addButtonToOrder(button); applyOrderToLayout(); if (wasEmpty) { emit emptyChanged(); } } void IdealButtonBarWidget::removeAction(QAction* widgetAction) { QWidget::removeAction(widgetAction); auto action = static_cast(widgetAction); action->button()->deleteLater(); delete action; - if (layout()->isEmpty()) { + if (m_buttonsLayout->isEmpty()) { emit emptyChanged(); } } bool IdealButtonBarWidget::isEmpty() const { return actions().isEmpty(); } bool IdealButtonBarWidget::isShown() const { const auto actions = this->actions(); return std::any_of(actions.cbegin(), actions.cend(), [](const QAction* action){ return action->isChecked(); }); } void IdealButtonBarWidget::saveShowState() { m_showState = isShown(); } bool IdealButtonBarWidget::lastShowState() { return m_showState; } QString IdealButtonBarWidget::id(const IdealToolButton* button) const { foreach (QAction* a, actions()) { auto tva = dynamic_cast(a); if (tva && tva->button() == button) { return tva->id(); } } return QString(); } IdealToolButton* IdealButtonBarWidget::button(const QString& id) const { foreach (QAction* a, actions()) { auto tva = dynamic_cast(a); if (tva && tva->id() == id) { return tva->button(); } } return nullptr; } void IdealButtonBarWidget::addButtonToOrder(const IdealToolButton* button) { QString buttonId = id(button); if (!m_buttonsOrder.contains(buttonId)) { - if (m_area == Qt::BottomDockWidgetArea) { - m_buttonsOrder.push_front(buttonId); - } - else { - m_buttonsOrder.push_back(buttonId); - } + m_buttonsOrder.push_back(buttonId); } } void IdealButtonBarWidget::loadOrderSettings(const KConfigGroup& configGroup) { m_buttonsOrder = configGroup.readEntry(QStringLiteral("(%1) Tool Views Order").arg(m_area), QStringList()); applyOrderToLayout(); } void IdealButtonBarWidget::saveOrderSettings(KConfigGroup& configGroup) { takeOrderFromLayout(); configGroup.writeEntry(QStringLiteral("(%1) Tool Views Order").arg(m_area), m_buttonsOrder); } bool IdealButtonBarWidget::isLocked() const { KConfigGroup config = KSharedConfig::openConfig()->group("UI"); return config.readEntry(QStringLiteral("Toolview Bar (%1) Is Locked").arg(m_area), false); } void IdealButtonBarWidget::applyOrderToLayout() { // If widget already have some buttons in the layout then calling loadOrderSettings() may leads // to situations when loaded order does not contains all existing buttons. Therefore we should // fix this with using addToOrder() method. - for (int i = 0; i < layout()->count(); ++i) { - if (auto button = dynamic_cast(layout()->itemAt(i)->widget())) { + for (int i = 0; i < m_buttonsLayout->count(); ++i) { + if (auto button = dynamic_cast(m_buttonsLayout->itemAt(i)->widget())) { addButtonToOrder(button); - layout()->removeWidget(button); + m_buttonsLayout->removeWidget(button); } } foreach(const QString& id, m_buttonsOrder) { if (auto b = button(id)) { - layout()->addWidget(b); + m_buttonsLayout->addWidget(b); } } } void IdealButtonBarWidget::takeOrderFromLayout() { m_buttonsOrder.clear(); - for (int i = 0; i < layout()->count(); ++i) { - if (auto button = dynamic_cast(layout()->itemAt(i)->widget())) { + for (int i = 0; i < m_buttonsLayout->count(); ++i) { + if (auto button = dynamic_cast(m_buttonsLayout->itemAt(i)->widget())) { m_buttonsOrder += id(button); } } } Qt::Orientation IdealButtonBarWidget::orientation() const { if (m_area == Qt::LeftDockWidgetArea || m_area == Qt::RightDockWidgetArea) return Qt::Vertical; return Qt::Horizontal; } Qt::DockWidgetArea IdealButtonBarWidget::area() const { return m_area; } void IdealButtonBarWidget::showWidget(bool checked) { Q_ASSERT(parentWidget() != nullptr); QAction *action = qobject_cast(sender()); Q_ASSERT(action); showWidget(action, checked); } void IdealButtonBarWidget::showWidget(QAction *action, bool checked) { auto widgetAction = static_cast(action); IdealToolButton* button = widgetAction->button(); Q_ASSERT(button); if (checked) { if ( !QApplication::keyboardModifiers().testFlag(Qt::ControlModifier) ) { // Make sure only one widget is visible at any time. // The alternative to use a QActionCollection and setting that to "exclusive" // has a big drawback: QActions in a collection that is exclusive cannot // be un-checked by the user, e.g. in the View -> Tool Views menu. foreach(QAction *otherAction, actions()) { if ( otherAction != widgetAction && otherAction->isChecked() ) otherAction->setChecked(false); } } m_controller->lastDockWidget[m_area] = widgetAction->dockWidget(); } m_controller->showDockWidget(widgetAction->dockWidget(), checked); widgetAction->setChecked(checked); button->setChecked(checked); } IdealDockWidget * IdealButtonBarWidget::widgetForAction(QAction *_action) const { return static_cast(_action)->dockWidget(); } #include "idealbuttonbarwidget.moc" diff --git a/kdevplatform/sublime/idealbuttonbarwidget.h b/kdevplatform/sublime/idealbuttonbarwidget.h index db0f927191..011466557f 100644 --- a/kdevplatform/sublime/idealbuttonbarwidget.h +++ b/kdevplatform/sublime/idealbuttonbarwidget.h @@ -1,100 +1,102 @@ /* Copyright 2007 Roberto Raggi Copyright 2007 Hamish Rodda Copyright 2011 Alexander Dymo Permission to use, copy, modify, distribute, and sell this software and its documentation for any purpose is hereby granted without fee, provided that the above copyright notice appear in all copies and that both that copyright notice and this permission notice appear in supporting documentation. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE KDEVELOP TEAM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #ifndef IDEALBUTTONBARWIDGET_H #define IDEALBUTTONBARWIDGET_H #include class IdealToolButton; class ToolViewAction; class QAction; class KConfigGroup; namespace Sublime { class MainWindow; class IdealController; class IdealDockWidget; +class IdealButtonBarLayout; class View; class Area; class IdealButtonBarWidget: public QWidget { Q_OBJECT public: IdealButtonBarWidget(Qt::DockWidgetArea area, IdealController *controller, Sublime::MainWindow *parent); QAction* addWidget(IdealDockWidget *widget, Area* area, View *view); void addAction(QAction *action); void removeAction(QAction* action); Qt::Orientation orientation() const; Qt::DockWidgetArea area() const; IdealDockWidget* widgetForAction(QAction* action) const; QWidget* corner() const; void showWidget(QAction *widgetAction, bool checked); bool isEmpty() const; bool isShown() const; void saveShowState(); bool lastShowState(); void loadOrderSettings(const KConfigGroup& configGroup); void saveOrderSettings(KConfigGroup& configGroup); bool isLocked() const; Q_SIGNALS: void emptyChanged(); private: void showWidget(bool checked); void applyOrderToLayout(); void takeOrderFromLayout(); IdealToolButton* button(const QString& id) const; QString id(const IdealToolButton* button) const; void addButtonToOrder(const IdealToolButton* button); Qt::DockWidgetArea m_area; IdealController* m_controller; QWidget* m_corner; bool m_showState; QStringList m_buttonsOrder; + IdealButtonBarLayout* m_buttonsLayout; }; } #endif // IDEALBUTTONBARWIDGET_H diff --git a/kdevplatform/sublime/ideallayout.cpp b/kdevplatform/sublime/ideallayout.cpp index 6c6efa57dc..8bc4347919 100644 --- a/kdevplatform/sublime/ideallayout.cpp +++ b/kdevplatform/sublime/ideallayout.cpp @@ -1,259 +1,306 @@ /* Copyright 2007 Roberto Raggi Copyright 2007 Hamish Rodda Copyright 2008 Vladimir Prus Permission to use, copy, modify, distribute, and sell this software and its documentation for any purpose is hereby granted without fee, provided that the above copyright notice appear in all copies and that both that copyright notice and this permission notice appear in supporting documentation. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE KDEVELOP TEAM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include "ideallayout.h" #include #include +#include + using namespace Sublime; IdealButtonBarLayout::IdealButtonBarLayout(Qt::Orientation orientation, QWidget *parent) : QLayout(parent) , _orientation(orientation) , _height(0) { setContentsMargins(0, 0, 0, 0); invalidate(); } void IdealButtonBarLayout::invalidate() { m_minSizeDirty = true; m_sizeHintDirty = true; m_layoutDirty = true; QLayout::invalidate(); } IdealButtonBarLayout::~IdealButtonBarLayout() { qDeleteAll(_items); } void IdealButtonBarLayout::setHeight(int height) { Q_ASSERT(orientation() == Qt::Vertical); _height = height; (void) invalidate(); } Qt::Orientation IdealButtonBarLayout::orientation() const { return _orientation; } Qt::Orientations IdealButtonBarLayout::expandingDirections() const { return orientation(); } QSize IdealButtonBarLayout::minimumSize() const { // The code below appears to be completely wrong -- // it will return the maximum size of a single button, not any // estimate as to how much space is necessary to draw all buttons // in a minimally acceptable way. if (m_minSizeDirty) { if (orientation() == Qt::Vertical) { const int width = doVerticalLayout(QRect(0, 0, 0, _height), false); return QSize(width, 0); } m_min = QSize(0, 0); for (QLayoutItem* item : _items) { m_min = m_min.expandedTo(item->minimumSize()); } m_minSizeDirty = false; } return m_min; } QSize IdealButtonBarLayout::sizeHint() const { if (m_sizeHintDirty) { const int buttonSpacing = this->buttonSpacing(); int orientationSize = 0; int crossSize = 0; bool first = true; for (QLayoutItem *item : _items) { QSize hint = item->sizeHint(); int orientationSizeHere; int crossSizeHere; if (orientation() == Qt::Vertical) { orientationSizeHere = hint.height(); crossSizeHere = hint.width(); } else { orientationSizeHere = hint.width(); crossSizeHere = hint.height(); } if (first) { crossSize = crossSizeHere; } else { orientationSize += buttonSpacing; } orientationSize += orientationSizeHere; first = false; } if (orientation() == Qt::Vertical) m_hint = QSize(crossSize, orientationSize); else m_hint = QSize(orientationSize, crossSize); if (!_items.empty()) { /* If we have no items, just use (0, 0) as hint, don't append any margins. */ int l, t, r, b; getContentsMargins(&l, &t, &r, &b); m_hint += QSize(l+r, t+b); } m_sizeHintDirty = false; } return m_hint; } void IdealButtonBarLayout::setGeometry(const QRect &rect) { if (m_layoutDirty || rect != geometry()) { if (orientation() == Qt::Vertical) doVerticalLayout(rect); else doHorizontalLayout(rect); } } void IdealButtonBarLayout::addItem(QLayoutItem *item) { _items.append(item); invalidate(); } QLayoutItem* IdealButtonBarLayout::itemAt(int index) const { return _items.value(index, nullptr); } QLayoutItem* IdealButtonBarLayout::takeAt(int index) { if (index >= 0 && index < _items.count()) return _items.takeAt(index); invalidate(); return nullptr; } int IdealButtonBarLayout::count() const { return _items.count(); } int IdealButtonBarLayout::buttonSpacing() const { auto pw = parentWidget(); return pw ? pw->style()->pixelMetric(QStyle::PM_ToolBarItemSpacing) : 0; } int IdealButtonBarLayout::doVerticalLayout(const QRect &rect, bool updateGeometry) const { const int buttonSpacing = this->buttonSpacing(); int l, t, r, b; getContentsMargins(&l, &t, &r, &b); int x = rect.x() + l; int y = rect.y() + t; int currentLineWidth = 0; - for (QLayoutItem *item : _items) { - const QSize itemSizeHint = item->sizeHint(); - if (y + itemSizeHint.height() + b > rect.height()) { - int newX = x + currentLineWidth + buttonSpacing; - if (newX + itemSizeHint.width() + r <= rect.width()) - { - x += currentLineWidth + buttonSpacing; - y = rect.y() + t; + if (_items.empty()) { + return x + currentLineWidth + r; + } + + const bool shrink = rect.height() < sizeHint().height(); + + const int maximumHeight = rect.height() / _items.size(); + int shrinkedHeight = -1; + + if (shrink) { + int smallItemCount = 0; + const int surplus = std::accumulate(_items.begin(), _items.end(), 0, [maximumHeight, &smallItemCount](int acc, QLayoutItem* item) { + const int itemHeight = item->sizeHint().height(); + if (itemHeight <= maximumHeight) { + acc += maximumHeight - itemHeight; + ++smallItemCount; } + return acc; + }); + + Q_ASSERT(_items.size() != smallItemCount); // should be true since rect.width != sizeHint.width + // evenly distribute surplus height over large items + shrinkedHeight = maximumHeight + surplus / (_items.size() - smallItemCount); + } + + for (QLayoutItem* item : _items) { + const QSize itemSizeHint = item->sizeHint(); + const int itemWidth = itemSizeHint.width(); + int itemHeight = itemSizeHint.height(); + + if (shrink && itemSizeHint.height() > maximumHeight) { + itemHeight = shrinkedHeight; } - if (updateGeometry) - item->setGeometry(QRect(x, y, itemSizeHint.width(), itemSizeHint.height())); + if (updateGeometry) { + item->setGeometry(QRect(x, y, itemWidth, itemHeight)); + } - currentLineWidth = qMax(currentLineWidth, itemSizeHint.width()); + currentLineWidth = qMax(currentLineWidth, itemWidth); - y += itemSizeHint.height() + buttonSpacing; + y += itemHeight + buttonSpacing; } m_layoutDirty = updateGeometry; return x + currentLineWidth + r; } int IdealButtonBarLayout::doHorizontalLayout(const QRect &rect, bool updateGeometry) const { const int buttonSpacing = this->buttonSpacing(); int l, t, r, b; getContentsMargins(&l, &t, &r, &b); int x = rect.x() + l; int y = rect.y() + t; int currentLineHeight = 0; - for (QLayoutItem *item : _items) { - QSize itemSizeHint = item->sizeHint(); - if (x + itemSizeHint.width() + r > rect.width()) { - // Run out of horizontal space. Try to move button to another - // row. - int newY = y + currentLineHeight + buttonSpacing; - if (newY + itemSizeHint.height() + b <= rect.height()) - { - y = newY; - x = rect.x() + l; - currentLineHeight = 0; + if (_items.empty()) { + return y + currentLineHeight + b; + } + + const bool shrink = rect.width() < sizeHint().width(); + + const int maximumWidth = rect.width() / _items.size(); + int shrinkedWidth = -1; + + if (shrink) { + int smallItemCount = 0; + const int surplus = std::accumulate(_items.begin(), _items.end(), 0, [maximumWidth, &smallItemCount](int acc, QLayoutItem* item) { + const int itemWidth = item->sizeHint().width(); + if (itemWidth <= maximumWidth) { + acc += maximumWidth - itemWidth; + ++smallItemCount; } + return acc; + }); + + Q_ASSERT(_items.size() != smallItemCount); // should be true since rect.width != sizeHint.width + // evenly distribute surplus width on the large items + shrinkedWidth = maximumWidth + surplus / (_items.size() - smallItemCount); + } + + for (QLayoutItem* item : _items) { + const QSize itemSizeHint = item->sizeHint(); + int itemWidth = itemSizeHint.width(); + const int itemHeight = itemSizeHint.height(); + + if (shrink && itemSizeHint.width() > maximumWidth) { + itemWidth = shrinkedWidth; } - if (updateGeometry) - item->setGeometry(QRect(x, y, itemSizeHint.width(), itemSizeHint.height())); + if (updateGeometry) { + item->setGeometry(QRect(x, y, itemWidth, itemHeight)); + } - currentLineHeight = qMax(currentLineHeight, itemSizeHint.height()); + currentLineHeight = qMax(currentLineHeight, itemHeight); - x += itemSizeHint.width() + buttonSpacing; + x += itemWidth + buttonSpacing; } m_layoutDirty = updateGeometry; return y + currentLineHeight + b; } diff --git a/kdevplatform/sublime/idealtoolbutton.cpp b/kdevplatform/sublime/idealtoolbutton.cpp index 42daad8665..01dbfefe24 100644 --- a/kdevplatform/sublime/idealtoolbutton.cpp +++ b/kdevplatform/sublime/idealtoolbutton.cpp @@ -1,133 +1,151 @@ /* Copyright 2007 Roberto Raggi Copyright 2007 Hamish Rodda Copyright 2011 Alexander Dymo Permission to use, copy, modify, distribute, and sell this software and its documentation for any purpose is hereby granted without fee, provided that the above copyright notice appear in all copies and that both that copyright notice and this permission notice appear in supporting documentation. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE KDEVELOP TEAM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include "idealtoolbutton.h" #include #include #include IdealToolButton::IdealToolButton(Qt::DockWidgetArea area, QWidget *parent) : QToolButton(parent), _area(area) { setFocusPolicy(Qt::NoFocus); KAcceleratorManager::setNoAccel(this); setCheckable(true); setAutoRaise(true); setToolButtonStyle(Qt::ToolButtonTextBesideIcon); setContextMenuPolicy(Qt::CustomContextMenu); } Qt::Orientation IdealToolButton::orientation() const { if (_area == Qt::LeftDockWidgetArea || _area == Qt::RightDockWidgetArea) return Qt::Vertical; return Qt::Horizontal; } QSize IdealToolButton::sizeHint() const { ensurePolished(); QStyleOptionToolButton opt; initStyleOption(&opt); QFontMetrics fm = fontMetrics(); const int charWidth = fm.width(QLatin1Char('x')); QSize textSize; // Use text size only if we request text if (toolButtonStyle() != Qt::ToolButtonIconOnly || opt.icon.isNull()) { textSize = fm.size(Qt::TextShowMnemonic, opt.text); textSize.rwidth() += 2 * charWidth; } int iconwidth = 0, iconheight = 0; // Use icon size only if it's requested and the icon is valid if (toolButtonStyle() != Qt::ToolButtonTextOnly && !opt.icon.isNull()) { if (_area == Qt::TopDockWidgetArea || _area == Qt::BottomDockWidgetArea) { iconwidth = opt.iconSize.width(); iconheight = opt.iconSize.height(); } else { iconwidth = opt.iconSize.height(); iconheight = opt.iconSize.width(); } } // adding +4 to be consistent with qtoolbutton int width = textSize.width() + iconwidth + 4; int height = qMax(textSize.height(), iconheight); QSize size = style()->sizeFromContents(QStyle::CT_ToolButton, &opt, QSize(width, height), this); if (orientation() == Qt::Vertical) { return QSize(size.height(), size.width()); // transposed } else { return size; } } void IdealToolButton::paintEvent(QPaintEvent *event) { + Q_UNUSED(event); + + QStylePainter painter(this); + QStyleOptionToolButton option; + initStyleOption(&option); + if (_area == Qt::TopDockWidgetArea || _area == Qt::BottomDockWidgetArea) { - QToolButton::paintEvent(event); + // elide text + int iconWidth = 0; + if (toolButtonStyle() != Qt::ToolButtonTextOnly && !option.icon.isNull()) { + iconWidth = option.iconSize.width(); + } + + // subtract 4 to be consistent with the size calculated by sizeHint, which adds 4, + // again to be consistent with QToolButton + option.text = fontMetrics().elidedText(text(), Qt::ElideRight, contentsRect().width() - iconWidth - 4); + painter.drawComplexControl(QStyle::CC_ToolButton, option); } else { // rotated paint - QStylePainter painter(this); - QStyleOptionToolButton option; - initStyleOption(&option); - + // elide text + int iconHeight = 0; + if (toolButtonStyle() != Qt::ToolButtonTextOnly && !option.icon.isNull()) { + iconHeight = option.iconSize.height(); + } + QString textToDraw = fontMetrics().elidedText(text(), Qt::ElideRight, contentsRect().height() - iconHeight - 4); // first draw normal frame and not text/icon option.text = QString(); option.icon = QIcon(); painter.drawComplexControl(QStyle::CC_ToolButton, option); // rotate the options QSize size( option.rect.size() ); size.transpose(); option.rect.setSize( size ); // rotate the painter if(_area == Qt::LeftDockWidgetArea) { painter.translate( 0, height() ); painter.rotate( -90 ); } else { painter.translate( width(), 0 ); painter.rotate( 90 ); } // paint text and icon - option.text = text(); + option.text = textToDraw; + QIcon::Mode iconMode = (option.state & QStyle::State_MouseOver) ? QIcon::Active : QIcon::Normal; QPixmap ic = icon().pixmap(option.iconSize, iconMode, QIcon::On); QTransform tf; if(_area == Qt::LeftDockWidgetArea) { tf = tf.rotate(90); } else { tf = tf.rotate(-90); } option.icon = ic.transformed( tf, Qt::SmoothTransformation ); painter.drawControl(QStyle::CE_ToolButtonLabel, option); painter.end(); } }