1 /*
2  * LibrePCB - Professional EDA for everyone!
3  * Copyright (C) 2013 LibrePCB Developers, see AUTHORS.md for contributors.
4  * https://librepcb.org/
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 /*******************************************************************************
21  *  Includes
22  ******************************************************************************/
23 #include "textpropertiesdialog.h"
24 
25 #include "../geometry/cmd/cmdtextedit.h"
26 #include "../geometry/text.h"
27 #include "../graphics/graphicslayer.h"
28 #include "../undostack.h"
29 #include "ui_textpropertiesdialog.h"
30 
31 #include <QtCore>
32 #include <QtWidgets>
33 
34 /*******************************************************************************
35  *  Namespace
36  ******************************************************************************/
37 namespace librepcb {
38 
TextPropertiesDialog(Text & text,UndoStack & undoStack,QList<GraphicsLayer * > layers,const LengthUnit & lengthUnit,const QString & settingsPrefix,QWidget * parent)39 TextPropertiesDialog::TextPropertiesDialog(Text& text, UndoStack& undoStack,
40                                            QList<GraphicsLayer*> layers,
41                                            const LengthUnit& lengthUnit,
42                                            const QString& settingsPrefix,
43                                            QWidget* parent) noexcept
44   : QDialog(parent),
45     mText(text),
46     mUndoStack(undoStack),
47     mUi(new Ui::TextPropertiesDialog) {
48   mUi->setupUi(this);
49   mUi->edtHeight->configure(lengthUnit, LengthEditBase::Steps::textHeight(),
50                             settingsPrefix % "/height");
51   mUi->edtPosX->configure(lengthUnit, LengthEditBase::Steps::generic(),
52                           settingsPrefix % "/pos_x");
53   mUi->edtPosY->configure(lengthUnit, LengthEditBase::Steps::generic(),
54                           settingsPrefix % "/pos_y");
55   mUi->edtRotation->setSingleStep(90.0);  // [°]
56 
57   foreach (const GraphicsLayer* layer, layers) {
58     mUi->cbxLayer->addItem(layer->getNameTr(), layer->getName());
59   }
60 
61   connect(mUi->buttonBox, &QDialogButtonBox::clicked, this,
62           &TextPropertiesDialog::on_buttonBox_clicked);
63 
64   // load text attributes
65   selectLayerNameInCombobox(*mText.getLayerName());
66   mUi->edtText->setPlainText(mText.getText());
67   mUi->alignmentSelector->setAlignment(mText.getAlign());
68   mUi->edtHeight->setValue(mText.getHeight());
69   mUi->edtPosX->setValue(mText.getPosition().getX());
70   mUi->edtPosY->setValue(mText.getPosition().getY());
71   mUi->edtRotation->setValue(mText.getRotation());
72 
73   // set focus to text so the user can immediately start typing to change it
74   mUi->edtText->selectAll();
75   mUi->edtText->setFocus();
76 }
77 
~TextPropertiesDialog()78 TextPropertiesDialog::~TextPropertiesDialog() noexcept {
79 }
80 
81 /*******************************************************************************
82  *  Setters
83  ******************************************************************************/
84 
setReadOnly(bool readOnly)85 void TextPropertiesDialog::setReadOnly(bool readOnly) noexcept {
86   mUi->cbxLayer->setDisabled(readOnly);
87   mUi->edtText->setReadOnly(readOnly);
88   mUi->alignmentSelector->setReadOnly(readOnly);
89   mUi->edtHeight->setReadOnly(readOnly);
90   mUi->edtPosX->setReadOnly(readOnly);
91   mUi->edtPosY->setReadOnly(readOnly);
92   mUi->edtRotation->setReadOnly(readOnly);
93   if (readOnly) {
94     mUi->buttonBox->setStandardButtons(QDialogButtonBox::StandardButton::Close);
95   } else {
96     mUi->buttonBox->setStandardButtons(
97         QDialogButtonBox::StandardButton::Apply |
98         QDialogButtonBox::StandardButton::Cancel |
99         QDialogButtonBox::StandardButton::Ok);
100   }
101 }
102 
103 /*******************************************************************************
104  *  Private Methods
105  ******************************************************************************/
106 
on_buttonBox_clicked(QAbstractButton * button)107 void TextPropertiesDialog::on_buttonBox_clicked(QAbstractButton* button) {
108   switch (mUi->buttonBox->buttonRole(button)) {
109     case QDialogButtonBox::ApplyRole:
110       applyChanges();
111       break;
112     case QDialogButtonBox::AcceptRole:
113       if (applyChanges()) {
114         accept();
115       }
116       break;
117     case QDialogButtonBox::RejectRole:
118       reject();
119       break;
120     default:
121       Q_ASSERT(false);
122       break;
123   }
124 }
125 
applyChanges()126 bool TextPropertiesDialog::applyChanges() noexcept {
127   try {
128     QScopedPointer<CmdTextEdit> cmd(new CmdTextEdit(mText));
129     if (mUi->cbxLayer->currentIndex() >= 0 &&
130         mUi->cbxLayer->currentData().isValid()) {
131       cmd->setLayerName(
132           GraphicsLayerName(mUi->cbxLayer->currentData().toString()),
133           false);  // can throw
134     }
135     cmd->setText(mUi->edtText->toPlainText().trimmed(), false);
136     cmd->setAlignment(mUi->alignmentSelector->getAlignment(), false);
137     cmd->setHeight(mUi->edtHeight->getValue(), false);
138     cmd->setPosition(Point(mUi->edtPosX->getValue(), mUi->edtPosY->getValue()),
139                      false);
140     cmd->setRotation(mUi->edtRotation->getValue(), false);
141     mUndoStack.execCmd(cmd.take());
142     return true;
143   } catch (const Exception& e) {
144     QMessageBox::critical(this, tr("Error"), e.getMsg());
145     return false;
146   }
147 }
148 
selectLayerNameInCombobox(const QString & name)149 void TextPropertiesDialog::selectLayerNameInCombobox(
150     const QString& name) noexcept {
151   mUi->cbxLayer->setCurrentIndex(mUi->cbxLayer->findData(name));
152 }
153 
154 /*******************************************************************************
155  *  End of File
156  ******************************************************************************/
157 
158 }  // namespace librepcb
159