All Classes Namespaces Functions Variables Enumerations Properties Pages
statusbar.cpp
1 /*
2 
3 Pencil2D - Traditional Animation Software
4 Copyright (C) 2020 Jakob Gahde
5 
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; version 2 of the License.
9 
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14 
15 */
16 
17 #include <cmath>
18 #include <QComboBox>
19 #include <QLabel>
20 #include <QLineEdit>
21 
22 #include "editor.h"
23 #include "elidedlabel.h"
24 #include "layermanager.h"
25 #include "scribblearea.h"
26 #include "toolmanager.h"
27 #include "viewmanager.h"
28 
29 #include "statusbar.h"
30 
32 {
33  setContentsMargins(3, 0, 3, 0);
34 
35  mToolIcon = new QLabel(this);
37  mToolLabel = new ElidedLabel(this);
40 
41  mModifiedLabel = new QLabel(this);
42  mModifiedLabel->setPixmap(QPixmap(":/icons/save.png"));
43  updateModifiedStatus(false);
45 
47  mZoomBox = new QComboBox(this);
49  << locale.toString(10000., 'f', 1) + locale.percent()
50  << locale.toString(6400., 'f', 1) + locale.percent()
51  << locale.toString(1600., 'f', 1) + locale.percent()
52  << locale.toString(800., 'f', 1) + locale.percent()
53  << locale.toString(400., 'f', 1) + locale.percent()
54  << locale.toString(200., 'f', 1) + locale.percent()
55  << locale.toString(100., 'f', 1) + locale.percent()
56  << locale.toString(75., 'f', 1) + locale.percent()
57  << locale.toString(50., 'f', 1) + locale.percent()
58  << locale.toString(33., 'f', 1) + locale.percent()
59  << locale.toString(25., 'f', 1) + locale.percent()
60  << locale.toString(12., 'f', 1) + locale.percent()
61  << locale.toString(1., 'f', 1) + locale.percent());
63  mZoomBox->setEditable(true);
65  connect(mZoomBox, static_cast<void (QComboBox::*)(const QString &)>(&QComboBox::activated), [=](const QString &currentText)
66  {
67  if (mZoomBox->count() == mZoomBox->maxCount())
68  {
69  // Keep the size of the list reasonable by preventing user entries
70  // insertPolicy is unsuitable as it prevents entering custom values at all
72  }
73  emit zoomChanged(locale.toDouble(QString(currentText).remove(locale.percent())) / 100);
74  });
76 
77  mZoomSlider = new QSlider(Qt::Horizontal, this);
79  mZoomSlider->setRange(-20, 20);
82  connect(mZoomSlider, &QSlider::valueChanged, [this](int value)
83  {
84  emit zoomChanged(std::pow(10, value / 10.));
85  });
87 }
88 
89 void StatusBar::updateToolStatus(ToolType tool)
90 {
91  Q_ASSERT(mEditor);
92  switch (tool) {
93  case PENCIL:
94  mToolLabel->setText(tr("Click to draw. Hold Ctrl and Shift to erase or Alt to select a color from the canvas."));
95  break;
96  case ERASER:
97  mToolLabel->setText(tr("Click to erase."));
98  break;
99  case SELECT:
100  mToolLabel->setText(tr("Click and drag to create or modify a selection. Hold Alt to modify its contents or press Backspace to clear them."));
101  break;
102  case MOVE:
103  mToolLabel->setText(tr("Click and drag to move an object. Hold Ctrl to rotate."));
104  break;
105  case HAND:
106  mToolLabel->setText(tr("Click and drag to pan. Hold Ctrl to zoom or Alt to rotate."));
107  break;
108  case SMUDGE:
109  mToolLabel->setText(tr("Click to liquefy pixels or modify a vector line. Hold Alt to smooth."));
110  break;
111  case PEN:
112  mToolLabel->setText(tr("Click to draw. Hold Ctrl and Shift to erase or Alt to select a color from the canvas."));
113  break;
114  case POLYLINE:
115  if (mEditor->tools()->getTool(tool)->isActive())
116  {
117  mToolLabel->setText(tr("Click to continue the polyline. Double-click or press enter to complete the line or press Escape to discard it."));
118  }
119  else
120  {
121  mToolLabel->setText(tr("Click to create a new polyline. Hold Ctrl and Shift to erase."));
122  }
123  break;
124  case BUCKET:
125  mToolLabel->setText(tr("Click to fill an area with the current color. Hold Alt to select a color from the canvas."));
126  break;
127  case EYEDROPPER:
128  mToolLabel->setText(tr("Click to select a color from the canvas."));
129  break;
130  case BRUSH:
131  mToolLabel->setText(tr("Click to paint. Hold Ctrl and Shift to erase or Alt to select a color from the canvas."));
132  break;
133  default:
134  Q_ASSERT(false);
135  }
136 
137  static QPixmap toolIcons[TOOL_TYPE_COUNT]{
138  {":icons/new/svg/pencil_detailed.svg"},
139  {":icons/new/svg/eraser_detailed.svg"},
140  {":icons/new/svg/selection.svg"},
141  {":icons/new/svg/arrow.svg"},
142  {":icons/new/svg/hand_detailed.svg"},
143  {":icons/new/svg/smudge_detailed.svg"},
144  {":icons/new/svg/pen_detailed.svg"},
145  {":icons/new/svg/line.svg"},
146  {":icons/new/svg/bucket_detailed.svg"},
147  {":icons/new/svg/eyedropper_detailed.svg"},
148  {":icons/new/svg/brush_detailed.svg"}
149  };
150  mToolIcon->setPixmap(toolIcons[tool]);
151  mToolIcon->setToolTip(BaseTool::TypeName(tool));
152 }
153 
155 {
156  mModifiedLabel->setDisabled(!modified);
157  if (modified)
158  {
159  mModifiedLabel->setToolTip(tr("This file has unsaved changes"));
160  }
161  else
162  {
163  mModifiedLabel->setToolTip(tr("This file has no unsaved changes"));
164  }
165 }
166 
168 {
169  Q_ASSERT(mEditor);
170 
171  QLocale locale;
173  mZoomBox->setCurrentText(locale.toString(mEditor->view()->scaling() * 100, 'f', 1) + locale.percent());
174 
176  mZoomSlider->setValue(static_cast<int>(std::round(std::log10(mEditor->view()->scaling()) * 10)));
177 }
void updateModifiedStatus(bool modified)
Updates the file modification status.
Definition: statusbar.cpp:154
QString toString(qlonglong i) const const
void setRange(int min, int max)
QSlider * mZoomSlider
Slider for adjusting the zoom level.
Definition: statusbar.h:96
void setPixmap(const QPixmap &)
StatusBar(QWidget *parent=nullptr)
Constructs a new status bar.
Definition: statusbar.cpp:31
void setText(const QString &text)
[0]
Definition: elidedlabel.cpp:74
void updateToolStatus(ToolType tool)
Updates the status bar with information about the current tool.
Definition: statusbar.cpp:89
void setEditable(bool editable)
QString tr(const char *sourceText, const char *disambiguation, int n)
double toDouble(const QString &s, bool *ok) const const
AlignRight
void updateZoomStatus()
Updates the zoom level displayed in the status bar.
Definition: statusbar.cpp:167
void addWidget(QWidget *widget, int stretch)
void activated(int index)
QComboBox * mZoomBox
Combo box for choosing pre-defined or custom zoom levels.
Definition: statusbar.h:94
QLabel * mModifiedLabel
Label indicating that the current file contains unsaved changes.
Definition: statusbar.h:92
void setTickPosition(QSlider::TickPosition position)
void setSizePolicy(QSizePolicy)
QLocale locale() const const
void setValue(int)
void setAlignment(Qt::Alignment flag)
QLineEdit * lineEdit() const const
void addPermanentWidget(QWidget *widget, int stretch)
void removeItem(int index)
ElidedLabel * mToolLabel
Label used to display a short help text for the current tool.
Definition: statusbar.h:90
QLabel * mToolIcon
Label used to display the icon of the current tool.
Definition: statusbar.h:88
QChar percent() const const
void zoomChanged(double scale)
This signal is sent when the user chooses a new zoom level through the status bar.
void valueChanged(int value)
void addItems(const QStringList &texts)
Horizontal
void setTickInterval(int ti)
void setToolTip(const QString &)
void setDisabled(bool disable)
void setMaxCount(int max)
void setCurrentText(const QString &text)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
Editor * mEditor
The editor associated with this status bar.
Definition: statusbar.h:85
virtual bool isActive()
Check if the tool is active.
Definition: basetool.cpp:253
void setContentsMargins(int left, int top, int right, int bottom)