diff --git a/libs/ui/widgets/kis_widget_chooser.cpp b/libs/ui/widgets/kis_widget_chooser.cpp index abc69561b6..02305e048d 100644 --- a/libs/ui/widgets/kis_widget_chooser.cpp +++ b/libs/ui/widgets/kis_widget_chooser.cpp @@ -1,287 +1,282 @@ /* * Copyright (c) 2011 Silvio Heinrich * * 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 Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "kis_widget_chooser.h" #include #include #include #include #include #include #include #include #include #include #include #include "kis_config.h" KisWidgetChooser::KisWidgetChooser(int id, QWidget* parent) : QFrame(parent) , m_chooserid(id) { // QFrame::setFrameStyle(QFrame::StyledPanel|QFrame::Raised); m_acceptIcon = KisIconUtils::loadIcon("list-add"); m_buttons = new QButtonGroup(); m_popup = new QFrame(0, Qt::Popup); m_arrowButton = new QToolButton(); m_popup->setFrameStyle(QFrame::Panel|QFrame::Raised); m_arrowButton->setFixedWidth(m_arrowButton->sizeHint().height()/2); m_arrowButton->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum); m_arrowButton->setAutoRaise(true); updateArrowIcon(); connect(m_arrowButton, SIGNAL(clicked(bool)), SLOT(slotButtonPressed())); } KisWidgetChooser::~KisWidgetChooser() { delete m_buttons; delete m_popup; delete m_arrowButton; } void KisWidgetChooser::updateArrowIcon() { QImage image(16, 16, QImage::Format_ARGB32); image.fill(0); QStylePainter painter(&image, this); QStyleOption option; option.rect = image.rect(); option.palette = palette(); option.state = QStyle::State_Enabled; option.palette.setBrush(QPalette::ButtonText, option.palette.text()); painter.setBrush(option.palette.text().color()); painter.setPen(option.palette.text().color()); painter.drawPrimitive(QStyle::PE_IndicatorArrowDown, option); m_arrowButton->setIcon(QIcon(QPixmap::fromImage(image))); } -void KisWidgetChooser::addWidget(const QString& id, const QString& label, QWidget* widget) +void KisWidgetChooser::addLabelWidget(const QString& id, const QString& label, QWidget* widget) { - if(id.isEmpty()) { - delete widget; - return; - } - removeWidget(id); if (label.isEmpty()) { m_widgets.push_back(Data(id, widget, 0)); } else { m_widgets.push_back(Data(id, widget, new QLabel(label))); } delete m_popup->layout(); m_popup->setLayout(createPopupLayout()); m_popup->adjustSize(); delete QWidget::layout(); QWidget::setLayout(createLayout()); } QLayout* KisWidgetChooser::createLayout() { QHBoxLayout* layout = new QHBoxLayout(); layout->setContentsMargins(0, 0, 0, 0); layout->setSpacing(0); for(Iterator i=m_widgets.begin(); i!=m_widgets.end(); ++i) { if(i->chosen) { if (i->label) { layout->addWidget(i->label); } layout->addWidget(i->widget); break; } } layout->addWidget(m_arrowButton); return layout; } QLayout* KisWidgetChooser::createPopupLayout() { QGridLayout* layout = new QGridLayout(); int row = 0; int idx = 0; layout->setContentsMargins(0, 0, 0, 0); layout->setSpacing(0); QButtonGroup* group = new QButtonGroup(); QList buttons = m_buttons->buttons(); for(Iterator i=m_widgets.begin(); i!=m_widgets.end(); ++i) { if(!i->chosen) { if(row == buttons.size()) { QToolButton* bn = new QToolButton(); m_acceptIcon = KisIconUtils::loadIcon("list-add"); bn->setIcon(m_acceptIcon); bn->setAutoRaise(true); buttons.push_back(bn); } if (i->label) { layout->addWidget(i->label , row, 0); layout->addWidget(i->widget , row, 1); layout->addWidget(buttons[row], row, 2); } else { layout->addWidget(i->widget , row, 0); layout->addWidget(buttons[row], row, 1); } group->addButton(buttons[row], idx); ++row; } ++idx; } for(int i=row; ichosen) { delete m_popup->layout(); m_popup->setLayout(createPopupLayout()); m_popup->adjustSize(); } else delete QWidget::layout(); if (data->label) { delete data->label; } delete data->widget; m_widgets.erase(data); } } QWidget* KisWidgetChooser::chooseWidget(const QString& id) { QWidget* chosenWidget = 0; for(Iterator i=m_widgets.begin(); i!=m_widgets.end(); ++i) { if(i->id == id) { chosenWidget = i->widget; i->chosen = true; } else i->chosen = false; } delete m_popup->layout(); m_popup->setLayout(createPopupLayout()); m_popup->adjustSize(); delete QWidget::layout(); QWidget::setLayout(createLayout()); KisConfig cfg(false); cfg.setToolbarSlider(m_chooserid, id); return chosenWidget; } QWidget* KisWidgetChooser::getWidget(const QString& id) const { ConstIterator data = std::find(m_widgets.begin(), m_widgets.end(), Data(id)); if(data != m_widgets.end()) return data->widget; return 0; } void KisWidgetChooser::showPopupWidget() { QSize popSize = m_popup->size(); QRect popupRect(QFrame::mapToGlobal(QPoint(-1, QFrame::height())), popSize); // Get the available geometry of the screen which contains this KisPopupButton QRect screenRect = QApplication::desktop()->availableGeometry(this); // Make sure the popup is not drawn outside the screen area if(popupRect.right() > screenRect.right()) popupRect.translate(screenRect.right() - popupRect.right(), 0); if(popupRect.left() < screenRect.left()) popupRect.translate(screenRect.left() - popupRect.left(), 0); if(popupRect.bottom() > screenRect.bottom()) popupRect.translate(0, -popupRect.height()); m_popup->setGeometry(popupRect); m_popup->show(); } void KisWidgetChooser::updateThemedIcons() { for (int i = 0; i < m_buttons->buttons().length(); i++) { if ( m_buttons->button(i)) { m_buttons->button(i)->setIcon(KisIconUtils::loadIcon("list-add")); } } } void KisWidgetChooser::slotButtonPressed() { showPopupWidget(); } void KisWidgetChooser::slotWidgetChosen(int index) { chooseWidget(m_widgets[index].id); m_popup->hide(); } void KisWidgetChooser::changeEvent(QEvent *e) { QFrame::changeEvent(e); switch (e->type()) { case QEvent::StyleChange: case QEvent::PaletteChange: case QEvent::EnabledChange: updateArrowIcon(); break; default: ; } } diff --git a/libs/ui/widgets/kis_widget_chooser.h b/libs/ui/widgets/kis_widget_chooser.h index 30c222e357..e8bfc6f519 100644 --- a/libs/ui/widgets/kis_widget_chooser.h +++ b/libs/ui/widgets/kis_widget_chooser.h @@ -1,106 +1,110 @@ /* * Copyright (c) 2011 Silvio Heinrich * * 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 Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifndef H_KIS_WIDGET_CHOOSER_H_ #define H_KIS_WIDGET_CHOOSER_H_ #include #include #include #include class QToolButton; class QLabel; class QButtonGroup; class KRITAUI_EXPORT KisWidgetChooser: public QFrame { Q_OBJECT struct Data { Data(const QString& ID): id(ID), widget(0), label(0), chosen(false) { } Data(const Data& d): id(d.id), widget(d.widget), label(d.label), chosen(d.chosen) { } Data(const QString& ID, QWidget* w, QLabel* l): id(ID), widget(w), label(l), chosen(false) { } friend bool operator == (const Data& a, const Data& b) { return a.id == b.id; } QString id; QWidget* widget; QLabel* label; bool chosen; }; typedef QList::iterator Iterator; typedef QList::const_iterator ConstIterator; public: KisWidgetChooser(int id, QWidget* parent=0); ~KisWidgetChooser() override; QWidget* chooseWidget(const QString& id); - void addWidget(const QString& id, const QString& label, QWidget* widget); + void addLabelWidget(const QString& id, const QString& label, QWidget* widget); QWidget* getWidget(const QString& id) const; template TWidget* addWidget(const QString& id, const QString& label = "") { + if (id.isEmpty()) { + return 0; + } + TWidget* widget = new TWidget(); - addWidget(id, label, widget); + addLabelWidget(id, label, widget); return widget; } template TWidget* getWidget(const QString& id) const { return dynamic_cast(getWidget(id)); } public Q_SLOTS: void showPopupWidget(); void updateThemedIcons(); private: void removeWidget(const QString& id); QLayout* createPopupLayout(); QLayout* createLayout(); void updateArrowIcon(); protected Q_SLOTS: void slotButtonPressed(); void slotWidgetChosen(int index); // QWidget interface protected: void changeEvent(QEvent *e) override; private: int m_chooserid; QIcon m_acceptIcon; QToolButton* m_arrowButton; QButtonGroup* m_buttons; QFrame* m_popup; QString m_chosenID; QList m_widgets; }; #endif // H_KIS_WIDGET_CHOOSER_H_