File indexing completed on 2024-04-28 05:38:15

0001 /***************************************************************************
0002  *  Copyright (C) 2009 by Renaud Guezennec                                 *
0003  *   https://rolisteam.org/contact                   *
0004  *                                                                         *
0005  *   rolisteam is free software; you can redistribute it and/or modify     *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  *                                                                         *
0010  *   This program is distributed in the hope that it will be useful,       *
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0013  *   GNU General Public License for more details.                          *
0014  *                                                                         *
0015  *   You should have received a copy of the GNU General Public License     *
0016  *   along with this program; if not, write to the                         *
0017  *   Free Software Foundation, Inc.,                                       *
0018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
0019  ***************************************************************************/
0020 
0021 #include <QtWidgets>
0022 
0023 #include "rwidgets/customs/diameterselector.h"
0024 #include "rwidgets/layouts/flowlayout.h"
0025 #include "rwidgets/toolbars/vtoolbar.h"
0026 
0027 #include "controller/view_controller/vectorialmapcontroller.h"
0028 #include "preferences/preferencesmanager.h"
0029 
0030 HiddingButton::HiddingButton(QWidget* w) : QToolButton(w) {}
0031 
0032 void HiddingButton::addAction(QAction* act)
0033 {
0034     setDefaultAction(act);
0035     connect(act, &QAction::visibleChanged, this, [this, act]() { setVisible(act->isVisible()); });
0036 }
0037 
0038 ToolBox::ToolBox(VectorialMapController* ctrl, QWidget* parent) : QWidget(parent), m_ctrl(ctrl)
0039 {
0040     setWindowTitle(tr("Tools"));
0041     setObjectName("Toolbar");
0042     setupUi();
0043 
0044     // UI to Ctrl
0045     connect(m_colorSelector, &VColorSelector::currentColorChanged, m_ctrl, &VectorialMapController::setToolColor);
0046     connect(m_lineDiameter, &DiameterSelector::diameterChanged, m_ctrl, &VectorialMapController::setPenSize);
0047     connect(m_editionModeCombo, QOverload<int>::of(&QComboBox::currentIndexChanged), this,
0048             [this](int b) { m_ctrl->setEditionMode(static_cast<Core::EditionMode>(b)); });
0049     connect(m_resetCountAct, &QAction::triggered, this,
0050             [this]()
0051             {
0052                 m_displayNPCCounter->display(1);
0053                 m_ctrl->setNpcNumber(1);
0054             });
0055     connect(m_npcNameTextEdit, &QLineEdit::textEdited, m_ctrl, &VectorialMapController::setNpcName);
0056     connect(m_toolsGroup, &QActionGroup::triggered, this,
0057             [this](QAction* act)
0058             {
0059                 auto tool= static_cast<Core::SelectableTool>(act->data().toInt());
0060                 m_ctrl->setTool(tool);
0061                 m_ruleAct->setChecked(tool == Core::RULE);
0062                 m_anchorAct->setChecked(tool == Core::ANCHOR);
0063                 m_pathAct->setChecked(tool == Core::PATH);
0064                 m_pipette->setChecked(tool == Core::PIPETTE);
0065                 m_highlighterAct->setChecked(tool == Core::HIGHLIGHTER);
0066                 m_pencilAct->setChecked(tool == Core::PEN);
0067                 m_lineAct->setChecked(tool == Core::LINE);
0068                 m_rectAct->setChecked(tool == Core::EMPTYRECT);
0069                 m_rectFillAct->setChecked(tool == Core::FILLRECT);
0070                 m_elipseAct->setChecked(tool == Core::EMPTYELLIPSE);
0071                 m_elipseFillAct->setChecked(tool == Core::FILLEDELLIPSE);
0072                 m_textAct->setChecked(tool == Core::TEXT);
0073                 m_handAct->setChecked(tool == Core::HANDLER);
0074                 m_addPCAct->setChecked(tool == Core::NonPlayableCharacter);
0075                 m_bucketAct->setChecked(tool == Core::BUCKET);
0076                 m_textWithBorderAct->setChecked(tool == Core::TEXTBORDER);
0077             });
0078     connect(m_opacitySlider, &QSlider::valueChanged, m_ctrl, &VectorialMapController::setOpacity);
0079 
0080     // Ctrl to UI
0081     connect(m_ctrl, &VectorialMapController::npcNumberChanged, m_displayNPCCounter,
0082             QOverload<int>::of(&QLCDNumber::display));
0083     connect(m_ctrl, &VectorialMapController::permissionChanged, this, &ToolBox::updateUi);
0084     connect(m_ctrl, &VectorialMapController::visibilityChanged, this,
0085             [this]()
0086             {
0087                 if(m_ctrl->visibility() != Core::VisibilityMode::FOGOFWAR)
0088                 {
0089                     m_editionModeCombo->setCurrentIndex(0);
0090                     m_ctrl->setEditionMode(Core::EditionMode::Painting);
0091                 }
0092                 updateUi();
0093             });
0094     connect(m_ctrl, &VectorialMapController::editionModeChanged, this, &ToolBox::updateUi);
0095     connect(m_ctrl, &VectorialMapController::toolColorChanged, m_colorSelector, &VColorSelector::setCurrentColor);
0096     // connect(m_ctrl, &VectorialMapController::opacityChanged, m_opacitySlider, &RealSlider::setRealValue);
0097     connect(m_ctrl, &VectorialMapController::editionModeChanged, this,
0098             [this](Core::EditionMode mode) { m_editionModeCombo->setCurrentIndex(static_cast<int>(mode)); });
0099 
0100     updateUi();
0101 }
0102 
0103 ToolBox::~ToolBox()
0104 {
0105     qDebug() << "Destructor Toolbox";
0106 }
0107 
0108 void ToolBox::setupUi()
0109 {
0110     m_centralWidget= new QWidget(this);
0111     createActions();
0112     makeTools();
0113     QHBoxLayout* lay= new QHBoxLayout();
0114     lay->setContentsMargins(QMargins());
0115     lay->addWidget(m_centralWidget);
0116     setLayout(lay);
0117 }
0118 
0119 void ToolBox::createActions()
0120 {
0121     m_toolsGroup= new QActionGroup(this);
0122 
0123     m_pencilAct= new QAction(QIcon::fromTheme("pen"), tr("Pen"), m_toolsGroup);
0124     m_pencilAct->setData(Core::PEN);
0125 
0126     m_lineAct= new QAction(QIcon::fromTheme("line"), tr("Line"), m_toolsGroup);
0127     m_lineAct->setData(Core::LINE);
0128 
0129     m_rectAct= new QAction(QIcon::fromTheme("emptyrectangle"), tr("Empty Rectangle"), m_toolsGroup);
0130     m_rectAct->setData(Core::EMPTYRECT);
0131 
0132     m_rectFillAct= new QAction(QIcon::fromTheme("filledrectangle"), tr("filled Rectangle"), m_toolsGroup);
0133     m_rectFillAct->setData(Core::FILLRECT);
0134 
0135     m_elipseAct= new QAction(QIcon::fromTheme("emptyellipse"), tr("Empty Ellipse"), m_toolsGroup);
0136     m_elipseAct->setData(Core::EMPTYELLIPSE);
0137 
0138     m_elipseFillAct= new QAction(QIcon::fromTheme("filledellipse"), tr("Filled Ellipse"), m_toolsGroup);
0139     m_elipseFillAct->setData(Core::FILLEDELLIPSE);
0140 
0141     m_textAct= new QAction(QIcon::fromTheme("text"), tr("Text"), m_toolsGroup);
0142     m_textAct->setData(Core::TEXT);
0143 
0144     m_handAct= new QAction(QIcon::fromTheme("hand"), tr("Hand"), m_toolsGroup);
0145     m_handAct->setData(Core::HANDLER);
0146     m_handAct->setShortcut(QKeySequence(QKeySequence::Cancel));
0147 
0148     m_addPCAct= new QAction(QIcon::fromTheme("add"), tr("Add NPC"), m_toolsGroup);
0149     m_addPCAct->setData(Core::NonPlayableCharacter);
0150 
0151     m_ruleAct= new QAction(QIcon::fromTheme("rule"), tr("Rule"), m_toolsGroup);
0152     m_ruleAct->setData(Core::RULE);
0153 
0154     m_pathAct= new QAction(QIcon::fromTheme("path"), tr("Path"), m_toolsGroup);
0155     m_pathAct->setData(Core::PATH);
0156     m_anchorAct= new QAction(QIcon::fromTheme("insert-link-2"), tr("Anchor"), m_toolsGroup);
0157     m_anchorAct->setData(Core::ANCHOR);
0158 
0159     m_pipette= new QAction(QIcon::fromTheme("pipettecursor"), tr("Pipette"), m_toolsGroup);
0160     m_pipette->setData(Core::PIPETTE);
0161 
0162     m_highlighterAct= new QAction(QIcon::fromTheme("marker-512"), tr("Highlighter"), m_toolsGroup);
0163     m_highlighterAct->setData(Core::HIGHLIGHTER);
0164 
0165     m_bucketAct= new QAction(QIcon::fromTheme("000000-paint-bucket-512"), tr("Paint Bucket"), m_toolsGroup);
0166     m_bucketAct->setData(Core::BUCKET);
0167 
0168     m_textWithBorderAct= new QAction(QIcon::fromTheme("textwithBorder"), tr("Text With Border"), m_toolsGroup);
0169     m_textWithBorderAct->setData(Core::TEXTBORDER);
0170 
0171     m_resetCountAct= new QAction(QIcon::fromTheme("view-refresh"), tr("Reset NPC counter"), this);
0172     m_resetCountAct->setObjectName("reset counter");
0173 
0174     m_pencilAct->setCheckable(true);
0175     m_lineAct->setCheckable(true);
0176     m_rectAct->setCheckable(true);
0177     m_rectFillAct->setCheckable(true);
0178     m_elipseAct->setCheckable(true);
0179     m_elipseFillAct->setCheckable(true);
0180     m_textAct->setCheckable(true);
0181     m_handAct->setCheckable(true);
0182     m_addPCAct->setCheckable(true);
0183     m_ruleAct->setCheckable(true);
0184     m_pathAct->setCheckable(true);
0185     m_pipette->setCheckable(true);
0186     m_highlighterAct->setCheckable(true);
0187     m_bucketAct->setCheckable(true);
0188     m_anchorAct->setCheckable(true);
0189     m_handAct->setChecked(true);
0190 
0191     addAction(m_pencilAct);
0192     addAction(m_lineAct);
0193     addAction(m_rectAct);
0194     addAction(m_rectFillAct);
0195     addAction(m_elipseAct);
0196     addAction(m_elipseFillAct);
0197     addAction(m_textAct);
0198     addAction(m_handAct);
0199     addAction(m_addPCAct);
0200     addAction(m_ruleAct);
0201     addAction(m_pathAct);
0202     addAction(m_highlighterAct);
0203     addAction(m_bucketAct);
0204     addAction(m_anchorAct);
0205     addAction(m_resetCountAct);
0206 }
0207 void ToolBox::makeTools()
0208 {
0209     m_opacitySlider= new RealSlider(this);
0210     m_opacitySlider->setOrientation(Qt::Horizontal);
0211     m_opacitySlider->setRealValue(1.0);
0212 
0213     auto* penButton= new HiddingButton(this);
0214     auto* lineButton= new HiddingButton(this);
0215     auto* emptyRectButton= new HiddingButton(this);
0216     auto* filledRectButton= new HiddingButton(this);
0217     auto* emptyEllipseButton= new HiddingButton(this);
0218     auto* filledEllipseButton= new HiddingButton(this);
0219     auto* textButton= new HiddingButton(this);
0220     auto* handleButton= new HiddingButton(this);
0221     auto* addNpcButton= new HiddingButton(this);
0222     auto* resetNpcNumberButton= new HiddingButton(this);
0223     auto* ruleButton= new HiddingButton(this);
0224     auto* pathButton= new HiddingButton(this);
0225     auto* anchorButton= new HiddingButton(this);
0226     auto* pipetteButton= new HiddingButton(this);
0227     auto* bucketButton= new HiddingButton(this);
0228     auto* highlighterButton= new HiddingButton(this);
0229 
0230     penButton->addAction(m_pencilAct);
0231     lineButton->addAction(m_lineAct);
0232     emptyRectButton->addAction(m_rectAct);
0233     filledRectButton->addAction(m_rectFillAct);
0234     emptyEllipseButton->addAction(m_elipseAct);
0235     filledEllipseButton->addAction(m_elipseFillAct);
0236     textButton->addAction(m_textAct);
0237     handleButton->addAction(m_handAct);
0238     addNpcButton->addAction(m_addPCAct);
0239     resetNpcNumberButton->addAction(m_resetCountAct);
0240     // m_resetCountAct->setIcon(QIcon::fromTheme("view-refresh"));
0241     ruleButton->addAction(m_ruleAct);
0242     pathButton->addAction(m_pathAct);
0243     pipetteButton->addAction(m_pipette);
0244     anchorButton->addAction(m_anchorAct);
0245     highlighterButton->addAction(m_highlighterAct);
0246     textButton->addAction(m_textWithBorderAct);
0247     bucketButton->addAction(m_bucketAct);
0248     //  unveilRect->setDefaultAction(m_unmaskRectAct);
0249 
0250     connect(ruleButton, &QToolButton::triggered, ruleButton, &QToolButton::setDefaultAction);
0251     connect(textButton, &QToolButton::triggered, textButton, &QToolButton::setDefaultAction);
0252     connect(filledRectButton, &QToolButton::triggered, filledRectButton, &QToolButton::setDefaultAction);
0253     connect(pathButton, &QToolButton::triggered, pathButton, &QToolButton::setDefaultAction);
0254 
0255     penButton->setAutoRaise(true);
0256     lineButton->setAutoRaise(true);
0257     emptyRectButton->setAutoRaise(true);
0258     filledRectButton->setAutoRaise(true);
0259     emptyEllipseButton->setAutoRaise(true);
0260     filledEllipseButton->setAutoRaise(true);
0261     textButton->setAutoRaise(true);
0262     handleButton->setAutoRaise(true);
0263     addNpcButton->setAutoRaise(true);
0264     resetNpcNumberButton->setAutoRaise(true);
0265     ruleButton->setAutoRaise(true);
0266     pathButton->setAutoRaise(true);
0267     anchorButton->setAutoRaise(true);
0268     pipetteButton->setAutoRaise(true);
0269     highlighterButton->setAutoRaise(true);
0270     bucketButton->setAutoRaise(true);
0271     //   unveilRect->setAutoRaise(true);
0272 
0273     auto pref= PreferencesManager::getInstance();
0274     auto iconSideSize= pref->value(QStringLiteral("IconSize"), 20).toInt();
0275     QSize iconSize(iconSideSize, iconSideSize);
0276     penButton->setIconSize(iconSize);
0277     lineButton->setIconSize(iconSize);
0278     emptyRectButton->setIconSize(iconSize);
0279     filledRectButton->setIconSize(iconSize);
0280     emptyEllipseButton->setIconSize(iconSize);
0281     filledEllipseButton->setIconSize(iconSize);
0282     textButton->setIconSize(iconSize);
0283     handleButton->setIconSize(iconSize);
0284     addNpcButton->setIconSize(iconSize);
0285     resetNpcNumberButton->setIconSize(iconSize);
0286     ruleButton->setIconSize(iconSize);
0287     pathButton->setIconSize(iconSize);
0288     pipetteButton->setIconSize(iconSize);
0289     highlighterButton->setIconSize(iconSize);
0290     bucketButton->setIconSize(iconSize);
0291 
0292     QVBoxLayout* toolsVerticalLayout= new QVBoxLayout();
0293     toolsVerticalLayout->setContentsMargins(QMargins());
0294     toolsVerticalLayout->setSpacing(0);
0295 
0296     FlowLayout* toolsLayout= new FlowLayout();
0297     toolsLayout->setSpacing(0);
0298     toolsLayout->setContentsMargins(QMargins());
0299     toolsLayout->addWidget(penButton);
0300     toolsLayout->addWidget(lineButton);
0301     toolsLayout->addWidget(emptyRectButton);
0302     toolsLayout->addWidget(filledRectButton);
0303     toolsLayout->addWidget(emptyEllipseButton);
0304     toolsLayout->addWidget(filledEllipseButton);
0305     toolsLayout->addWidget(textButton);
0306     toolsLayout->addWidget(handleButton);
0307     toolsLayout->addWidget(ruleButton);
0308     toolsLayout->addWidget(pathButton);
0309     toolsLayout->addWidget(anchorButton);
0310     toolsLayout->addWidget(pipetteButton);
0311     toolsLayout->addWidget(bucketButton);
0312     toolsLayout->addWidget(highlighterButton);
0313 
0314     m_npcNameTextEdit= new QLineEdit();
0315     m_npcNameTextEdit->setToolTip(tr("NPC Name"));
0316 
0317     m_displayNPCCounter= new QLCDNumber(2);
0318     m_displayNPCCounter->setSegmentStyle(QLCDNumber::Flat);
0319     m_displayNPCCounter->display(1);
0320     m_displayNPCCounter->setToolTip(tr("NPC's number"));
0321 
0322     m_colorSelector= new VColorSelector(this);
0323     m_editionModeCombo= new QComboBox();
0324 
0325     m_editionModeCombo->setFrame(false);
0326     m_editionModeCombo->addItem(QIcon(":/resources/images/pen.png"), tr("Normal"),
0327                                 static_cast<int>(Core::EditionMode::Painting));
0328     m_editionModeCombo->addItem(QIcon(":/resources/images/mask.png"), tr("Mask"),
0329                                 static_cast<int>(Core::EditionMode::Mask));
0330     m_editionModeCombo->addItem(QIcon(":/resources/images/eye.png"), tr("Unmask"),
0331                                 static_cast<int>(Core::EditionMode::Unmask));
0332 
0333     /* connect(m_editionModeCombo, QOverload<int>::of(&QComboBox::currentIndexChanged), this,
0334              [this, ruleButton, pipetteButton, bucketButton, addNpcButton, anchorButton, highlighterButton]() {
0335                  auto tool= m_ctrl->tool();
0336                  auto mode= static_cast<Core::EditionMode>(m_editionModeCombo->currentData().toInt());
0337                  bool painting= (mode == Core::EditionMode::Painting);
0338 
0339                  ruleButton->setVisible(painting);
0340                  pipetteButton->setVisible(painting);
0341                  bucketButton->setVisible(painting);
0342                  addNpcButton->setVisible(painting);
0343                  anchorButton->setVisible(painting);
0344                  highlighterButton->setVisible(painting);
0345 
0346                  if(tool == Core::RULE || tool == Core::PIPETTE || tool == Core::BUCKET
0347                     || tool == Core::NonPlayableCharacter || tool == Core::ANCHOR)
0348                      m_ctrl->setTool(Core::HANDLER);
0349              });*/
0350 
0351     FlowLayout* characterToolsLayout= new FlowLayout();
0352     characterToolsLayout->setContentsMargins(QMargins());
0353     characterToolsLayout->setSpacing(0);
0354     characterToolsLayout->addWidget(addNpcButton);
0355     characterToolsLayout->addWidget(resetNpcNumberButton);
0356     characterToolsLayout->addWidget(m_displayNPCCounter);
0357 
0358     m_lineDiameter= new DiameterSelector(m_centralWidget, true, 1, 45);
0359     m_lineDiameter->setDiameter(15);
0360     m_lineDiameter->setToolTip(tr("height of the pen"));
0361 
0362     toolsVerticalLayout->addWidget(m_colorSelector);
0363     QFrame* line= new QFrame();
0364     line->setFrameShape(QFrame::HLine);
0365     line->setFrameShadow(QFrame::Sunken);
0366     toolsVerticalLayout->addWidget(m_editionModeCombo);
0367     toolsVerticalLayout->addWidget(line);
0368     toolsVerticalLayout->addLayout(toolsLayout);
0369     toolsVerticalLayout->addWidget(m_lineDiameter);
0370     toolsVerticalLayout->addLayout(characterToolsLayout);
0371     toolsVerticalLayout->addWidget(m_npcNameTextEdit);
0372     m_opacityLabel= new QLabel(tr("Opacity:"), this);
0373     toolsVerticalLayout->addWidget(m_opacityLabel);
0374     toolsVerticalLayout->addWidget(m_opacitySlider);
0375 
0376     m_zoomSlider= std::make_unique<RealSlider>();
0377     m_zoomSlider->setOrientation(Qt::Horizontal);
0378     m_zoomSlider->setStart(0.01);
0379     m_zoomSlider->setEnd(5.0);
0380 
0381     m_zoomSpinBox= std::make_unique<QDoubleSpinBox>();
0382     m_zoomSpinBox->setMinimum(0.01);
0383     m_zoomSpinBox->setMaximum(5.0);
0384 
0385     m_smallScene= std::make_unique<QLabel>();
0386     /// m_smallScene->setScaledContents(true);
0387 
0388     connect(m_ctrl, &VectorialMapController::zoomLevelChanged, this,
0389             [this]()
0390             {
0391                 auto zl= m_ctrl->zoomLevel();
0392                 qDebug() << zl;
0393             });
0394     toolsVerticalLayout->addWidget(m_smallScene.get());
0395     toolsVerticalLayout->addStretch(1);
0396     m_centralWidget->setLayout(toolsVerticalLayout);
0397 }
0398 
0399 void ToolBox::updateUi()
0400 {
0401     if(!m_ctrl)
0402         return;
0403     auto mode= m_ctrl->permission();
0404     auto editionMode= m_ctrl->editionMode();
0405 
0406     auto isGm= m_ctrl->localGM();
0407 
0408     auto pcMove= mode == Core::PC_MOVE;
0409     auto pcAll= mode == Core::PC_ALL;
0410     auto painting= editionMode == Core::EditionMode::Painting;
0411     auto isFow= m_ctrl->visibility() == Core::VisibilityMode::FOGOFWAR;
0412 
0413     m_editionModeCombo->setVisible(isFow && (isGm || pcAll));
0414 
0415     m_pencilAct->setVisible(isGm || pcAll);
0416     m_lineAct->setVisible(isGm || pcAll);
0417     m_rectAct->setVisible(isGm || pcAll);
0418     m_rectFillAct->setVisible(isGm || pcAll);
0419     m_elipseAct->setVisible(isGm || pcAll);
0420     m_elipseFillAct->setVisible(isGm || pcAll);
0421     m_textAct->setVisible((isGm || pcAll) && painting);
0422     m_addPCAct->setVisible((isGm || pcAll) && painting);
0423     m_pipette->setVisible((isGm || pcAll) && painting);
0424     m_colorSelector->setVisible((isGm || pcAll) && painting);
0425     m_resetCountAct->setVisible((isGm || pcAll) && painting);
0426     m_pathAct->setVisible(isGm || pcAll);
0427     m_anchorAct->setVisible((isGm || pcAll) && painting);
0428     m_bucketAct->setVisible((isGm || pcAll) && painting);
0429     m_npcNameTextEdit->setVisible((isGm || pcAll) && painting);
0430     m_displayNPCCounter->setVisible((isGm || pcAll) && painting);
0431     m_opacityLabel->setVisible((isGm || pcAll) && painting);
0432     m_opacitySlider->setVisible((isGm || pcAll) && painting);
0433     m_textWithBorderAct->setVisible(isGm || pcAll);
0434     m_handAct->setVisible(isGm || pcAll || pcMove);
0435 }
0436 
0437 void ToolBox::setImage(const QPixmap& img)
0438 {
0439     if(m_smallScene)
0440         m_smallScene->setPixmap(img);
0441 }