Warning, file /office/skrooge/skgbasegui/skgboardwidget.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /***************************************************************************
0002  * SPDX-FileCopyrightText: 2022 S. MANKOWSKI stephane@mankowski.fr
0003  * SPDX-FileCopyrightText: 2022 G. DE BURE support@mankowski.fr
0004  * SPDX-License-Identifier: GPL-3.0-or-later
0005  ***************************************************************************/
0006 /** @file
0007  * This file is a generic for board widget.
0008  *
0009  * @author Stephane MANKOWSKI / Guillaume DE BURE
0010  */
0011 #include "skgboardwidget.h"
0012 
0013 #include <qdom.h>
0014 #include <qinputdialog.h>
0015 #include <qmenu.h>
0016 #include <qpushbutton.h>
0017 #include <qtoolbutton.h>
0018 #include <qwidgetaction.h>
0019 
0020 #include <klocalizedstring.h>
0021 
0022 #include "skgmainpanel.h"
0023 #include "skgtraces.h"
0024 #include "skgzoomselector.h"
0025 
0026 SKGBoardWidget::SKGBoardWidget(QWidget* iParent, SKGDocument* iDocument, const QString& iTitle, bool iZoomable)
0027     : SKGWidget(iParent, iDocument), m_menu(nullptr), m_zoomMenu(nullptr), m_zoomRatio(1.0), m_title(iTitle), m_titleDefault(iTitle)
0028 {
0029     SKGTRACEINFUNC(10)
0030 
0031     // Created widgets
0032     auto horizontalLayout = new QHBoxLayout(this);
0033     horizontalLayout->setSpacing(0);
0034     horizontalLayout->setContentsMargins(0, 0, 0, 0);
0035     m_frame = new QFrame(this);
0036     m_frame->setObjectName(QStringLiteral("frame"));
0037     m_frame->setFrameShape(QFrame::StyledPanel);
0038     m_frame->setFrameShadow(QFrame::Raised);
0039     m_gridLayout = new QGridLayout(m_frame);
0040     m_gridLayout->setSpacing(2);
0041     m_gridLayout->setContentsMargins(0, 0, 0, 0);
0042 
0043     m_toolButton = new QToolButton(m_frame);
0044     m_toolButton->setIconSize(QSize(16, 16));
0045     m_toolButton->setMaximumSize(QSize(22, 22));
0046     m_toolButton->setPopupMode(QToolButton::InstantPopup);
0047     m_toolButton->setAutoRaise(true);
0048     m_toolButton->hide();
0049 
0050     m_gridLayout->addWidget(m_toolButton, 0, 0, 1, 1);
0051 
0052     m_Title = new QLabel(m_frame);
0053     QFont boldFont = m_Title->font();
0054     boldFont.setBold(true);
0055     m_Title->setFont(boldFont);
0056     m_Title->setAlignment(Qt::AlignCenter);
0057     m_Title->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed));
0058 
0059     getDragWidget()->setCursor(QCursor(Qt::OpenHandCursor));
0060 
0061     m_gridLayout->addWidget(m_Title, 0, 1, 1, 1);
0062 
0063     m_line = new QFrame(m_frame);
0064     m_line->setFrameShape(QFrame::HLine);
0065     m_line->setFrameShadow(QFrame::Sunken);
0066 
0067     m_gridLayout->addWidget(m_line, 1, 0, 1, 2);
0068 
0069     horizontalLayout->addWidget(m_frame);
0070 
0071     // Add default actions
0072     auto w = new QWidget(this);
0073     auto hlayoutMove = new QHBoxLayout(w);
0074     hlayoutMove->setSpacing(2);
0075     hlayoutMove->setContentsMargins(0, 0, 0, 0);
0076 
0077     auto pbFirst = new QPushButton(w);
0078     pbFirst->setToolTip(i18nc("Move tooltip", "Move first"));
0079     pbFirst->setIcon(SKGServices::fromTheme(QStringLiteral("go-first-view")));
0080     pbFirst->setMaximumSize(QSize(22, 22));
0081     pbFirst->setFlat(true);
0082     connect(pbFirst, &QPushButton::clicked, this, &SKGBoardWidget::requestMoveFirst);
0083     hlayoutMove->addWidget(pbFirst);
0084 
0085     auto pbBefore = new QPushButton(w);
0086     pbBefore->setToolTip(i18nc("Move tooltip", "Move before"));
0087     pbBefore->setIcon(SKGServices::fromTheme(QStringLiteral("go-previous-view")));
0088     pbBefore->setMaximumSize(QSize(22, 22));
0089     pbBefore->setFlat(true);
0090     connect(pbBefore, &QPushButton::clicked, this, &SKGBoardWidget::requestMoveBefore);
0091     hlayoutMove->addWidget(pbBefore);
0092 
0093     auto pbDelete = new QPushButton(w);
0094     pbDelete->setToolTip(i18nc("Move tooltip", "Delete"));
0095     pbDelete->setIcon(SKGServices::fromTheme(QStringLiteral("edit-delete")));
0096     pbDelete->setMaximumSize(QSize(22, 22));
0097     pbDelete->setFlat(true);
0098     connect(pbDelete, &QPushButton::clicked, this, &SKGBoardWidget::requestRemove);
0099     hlayoutMove->addWidget(pbDelete);
0100 
0101     auto pbAfter = new QPushButton(w);
0102     pbAfter->setToolTip(i18nc("Move tooltip", "Move after"));
0103     pbAfter->setIcon(SKGServices::fromTheme(QStringLiteral("go-next-view")));
0104     pbAfter->setMaximumSize(QSize(22, 22));
0105     pbAfter->setFlat(true);
0106     connect(pbAfter, &QPushButton::clicked, this, &SKGBoardWidget::requestMoveAfter);
0107     hlayoutMove->addWidget(pbAfter);
0108 
0109     auto pbLast = new QPushButton(w);
0110     pbLast->setToolTip(i18nc("Move tooltip", "Move last"));
0111     pbLast->setIcon(SKGServices::fromTheme(QStringLiteral("go-last-view")));
0112     pbLast->setMaximumSize(QSize(22, 22));
0113     pbLast->setFlat(true);
0114     connect(pbLast, &QPushButton::clicked, this, &SKGBoardWidget::requestMoveLast);
0115     hlayoutMove->addWidget(pbLast);
0116 
0117     auto moveWidget = new QWidgetAction(this);
0118     moveWidget->setDefaultWidget(w);
0119     addAction(moveWidget);
0120 
0121     if (iZoomable) {
0122         m_zoomMenu = new SKGZoomSelector(this);
0123         m_zoomMenu->setResetValue(-10);
0124         m_zoomMenu->setValue(-10, false);
0125         connect(m_zoomMenu, &SKGZoomSelector::changed, this, &SKGBoardWidget::onZoom);
0126 
0127         auto zoomWidget = new QWidgetAction(this);
0128         zoomWidget->setDefaultWidget(m_zoomMenu);
0129         addAction(zoomWidget);
0130     }
0131 
0132     m_menuRename = new QAction(SKGServices::fromTheme(QStringLiteral("edit-rename")), i18nc("Verb, change the name of an item", "Rename"), this);
0133     connect(m_menuRename, &QAction::triggered, this, &SKGBoardWidget::onRenameTitle);
0134     addAction(m_menuRename);
0135 
0136     auto sep = new QAction(this);
0137     sep->setSeparator(true);
0138     addAction(sep);
0139 
0140     // Set main title
0141     setMainTitle(iTitle);
0142 
0143     // Set default icon
0144     m_toolButton->setIcon(SKGServices::fromTheme(QStringLiteral("configure")));
0145 }
0146 
0147 SKGBoardWidget::~SKGBoardWidget()
0148 {
0149     SKGTRACEINFUNC(10)
0150     m_menuRename = nullptr;
0151 }
0152 
0153 QWidget* SKGBoardWidget::getDragWidget()
0154 {
0155     return m_Title;
0156 }
0157 
0158 double SKGBoardWidget::getZoomRatio()
0159 {
0160     return m_zoomRatio;
0161 }
0162 
0163 void SKGBoardWidget::onZoom(int iZoom)
0164 {
0165     setZoomRatio((iZoom + 15.0) / 5.0);
0166 }
0167 
0168 void SKGBoardWidget::requestMoveAfter()
0169 {
0170     Q_EMIT requestMove(1);
0171 }
0172 
0173 void SKGBoardWidget::requestMoveBefore()
0174 {
0175     Q_EMIT requestMove(-1);
0176 }
0177 
0178 void SKGBoardWidget::requestMoveFirst()
0179 {
0180     Q_EMIT requestMove(-100000);
0181 }
0182 
0183 void SKGBoardWidget::requestMoveLast()
0184 {
0185     Q_EMIT requestMove(100000);
0186 }
0187 
0188 void SKGBoardWidget::setZoomRatio(double iRatio)
0189 {
0190     if (m_zoomMenu != nullptr) {
0191         if (m_zoomRatio == 1.0) {
0192             // Memorize initial size
0193             m_initialSize = minimumSize();
0194         }
0195 
0196         // Move zoom widget
0197         m_zoomRatio = iRatio;
0198         if (m_zoomRatio < 1.0) {
0199             m_zoomRatio = 1.0;
0200         } else if (m_zoomRatio > 5.0) {
0201             m_zoomRatio = 5.0;
0202         }
0203         m_zoomMenu->setValue(5.0 * iRatio - 15.0, false);
0204 
0205         // Resize widget
0206         QSize newSize(m_initialSize.width()*iRatio, m_initialSize.height()*iRatio);
0207         setMinimumSize(newSize);
0208     }
0209 }
0210 
0211 void SKGBoardWidget::setState(const QString& iState)
0212 {
0213     QDomDocument doc(QStringLiteral("SKGML"));
0214     if (doc.setContent(iState)) {
0215         QDomElement root = doc.documentElement();
0216 
0217         QString title = root.attribute(QStringLiteral("title"));
0218         if (!title.isEmpty()) {
0219             m_title = title;
0220             setMainTitle(title);
0221         }
0222     }
0223 }
0224 
0225 QString SKGBoardWidget::getState()
0226 {
0227     QDomDocument doc(QStringLiteral("SKGML"));
0228     QDomElement root = doc.createElement(QStringLiteral("parameters"));
0229     doc.appendChild(root);
0230 
0231     root.setAttribute(QStringLiteral("title"), m_title);
0232     return doc.toString();
0233 }
0234 
0235 void SKGBoardWidget::onRenameTitle()
0236 {
0237     bool ok = false;
0238     QString newTitle = QInputDialog::getText(SKGMainPanel::getMainPanel(), i18nc("Question", "Title"),
0239                        i18nc("Question", "New title (Empty to retrieve the default title):"), QLineEdit::Normal, getOriginalTitle(), &ok);
0240     if (ok) {
0241         m_title = newTitle;
0242         if (m_title.isEmpty()) {
0243             m_title = m_titleDefault;
0244         }
0245         setMainTitle(m_title);
0246     }
0247 }
0248 
0249 void SKGBoardWidget::setMainWidget(QWidget* iWidget)
0250 {
0251     iWidget->setParent(m_frame);
0252     m_gridLayout->addWidget(iWidget, 2, 0, 1, 2);
0253 }
0254 
0255 void SKGBoardWidget::insertAction(int iPos, QAction* iAction)
0256 {
0257     auto l = getMenu()->actions();
0258     getMenu()->insertAction(l[iPos], iAction);
0259 
0260     // Change icon
0261     if (!iAction->isCheckable() && !iAction->isSeparator()) {
0262         m_toolButton->setIcon(SKGServices::fromTheme(QStringLiteral("run-build-configure")));
0263     }
0264 }
0265 
0266 QMenu* SKGBoardWidget::getMenu()
0267 {
0268     if (m_menu == nullptr) {
0269         m_menu = new QMenu(this);
0270         m_toolButton->show();
0271         m_toolButton->setMenu(m_menu);
0272     }
0273 
0274     return m_menu;
0275 }
0276 
0277 void SKGBoardWidget::addMenu(QMenu* iMenu)
0278 {
0279     getMenu()->addMenu(iMenu);
0280 }
0281 
0282 void SKGBoardWidget::addAction(QAction* iAction)
0283 {
0284     getMenu()->addAction(iAction);
0285 
0286     // Change icon
0287     if (!iAction->isCheckable() && !iAction->isSeparator()) {
0288         m_toolButton->setIcon(SKGServices::fromTheme(QStringLiteral("run-build-configure")));
0289     }
0290 }
0291 
0292 void SKGBoardWidget::hideTitle()
0293 {
0294     m_toolButton->hide();
0295     m_Title->hide();
0296     m_line->hide();
0297 }
0298 
0299 void SKGBoardWidget::setMainTitle(const QString& iTitle)
0300 {
0301     m_Title->setText(iTitle);
0302 }
0303 
0304 QString SKGBoardWidget::getMainTitle()
0305 {
0306     return m_Title->text();
0307 }
0308 
0309 QString SKGBoardWidget::getOriginalTitle()
0310 {
0311     return m_title;
0312 }
0313 
0314 #include "skgboardwidget.h"