1
/*******************************************************************
3
Part of the Fritzing project - http://fritzing.org
4
Copyright (c) 2007-2010 Fachhochschule Potsdam - http://fh-potsdam.de
6
Fritzing 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.
11
Fritzing 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.
16
You should have received a copy of the GNU General Public License
17
along with Fritzing. If not, see <http://www.gnu.org/licenses/>.
19
********************************************************************
22
$Author: cohen@irascible.com $:
23
$Date: 2011-07-02 00:46:20 +0200 (Sat, 02 Jul 2011) $
25
********************************************************************/
30
#include <QTextStream>
32
#include <QHBoxLayout>
37
#include "zoomslider.h"
38
#include "../debugdialog.h"
40
qreal ZoomSlider::ZoomStep;
41
QList<qreal> ZoomSlider::ZoomFactors;
43
static const int MIN_VALUE = 10;
44
static const int MAX_VALUE = 2010;
45
static const int STARTING_VALUE = 100;
46
static const int HEIGHT = 16;
47
static const int STEP = 100;
49
static int AutoRepeatDelay = 0;
50
static int AutoRepeatInterval = 0;
52
///////////////////////////////////////////////
54
ZoomLabel::ZoomLabel(QWidget * parent) : QLabel(parent)
56
if (AutoRepeatDelay == 0) {
58
AutoRepeatInterval = button.autoRepeatInterval();
59
AutoRepeatDelay = button.autoRepeatDelay();
61
m_autoRepeat = m_mouseIsDown = m_mouseIsIn = m_repeated = false;
62
m_timer.setSingleShot(false);
63
m_timer.setInterval(AutoRepeatInterval);
64
connect(&m_timer, SIGNAL(timeout()), this, SLOT(repeat()));
67
ZoomLabel::~ZoomLabel()
71
void ZoomLabel::setAutoRepeat(bool autoRepeat)
73
m_autoRepeat = autoRepeat;
76
void ZoomLabel::setImages(const QString & normal, const QString & pressed)
78
m_normal.load(normal);
79
m_pressed.load(pressed);
84
void ZoomLabel::repeat()
86
if (m_mouseIsIn && m_mouseIsDown) {
92
void ZoomLabel::mousePressEvent(QMouseEvent * event)
95
this->setMouseTracking(true);
96
this->setPixmap(m_pressed);
97
QLabel::mousePressEvent(event);
98
m_mouseIsDown = m_mouseIsIn = true;
101
m_timer.start(AutoRepeatDelay);
105
void ZoomLabel::mouseMoveEvent(QMouseEvent * event)
107
QLabel::mouseMoveEvent(event);
109
QPoint p = event->pos();
110
bool mouseIsIn = p.x() >= 0 && p.y() >= 0 && p.x() <= width() && p.y() <= height();
111
if (m_mouseIsIn && !mouseIsIn) {
113
this->setPixmap(m_normal);
116
else if (!m_mouseIsIn && mouseIsIn) {
117
this->setPixmap(m_pressed);
119
m_timer.start(AutoRepeatDelay);
124
void ZoomLabel::mouseReleaseEvent(QMouseEvent * event)
127
this->setMouseTracking(false);
128
m_mouseIsDown = false;
129
this->setPixmap(m_normal);
130
QLabel::mouseReleaseEvent(event);
131
if (!m_repeated && m_mouseIsIn) {
136
/////////////////////////////////////////////
138
ZoomSlider::ZoomSlider(QWidget * parent) : QFrame(parent)
140
// layout doesn't seem to work: the slider appears too far down in the status bar
141
// because the status bar layout is privileged for the message text
144
if (ZoomFactors.size() == 0) {
148
this->setObjectName("ZoomSliderFrame");
151
m_lineEdit = new QLineEdit(this);
152
m_lineEdit->setObjectName("ZoomSliderValue");
153
m_lineEdit->setText(QString("%1").arg(STARTING_VALUE));
154
m_lineEdit->setValidator(new QIntValidator(MIN_VALUE, MAX_VALUE, this));
155
m_lineEdit->setAttribute(Qt::WA_MacShowFocusRect, 0);
156
m_lineEdit->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
158
m_suffix = new QLabel(tr("%"), this);
159
m_suffix->setObjectName("ZoomSliderLabel");
161
m_minusButton = new ZoomLabel(this);
162
m_minusButton->setImages(":/resources/images/icons/zoomSliderMinus.png", ":/resources/images/icons/zoomSliderMinusPressed.png");
163
m_minusButton->setAutoRepeat(true);
164
m_minusButton->setObjectName("ZoomSliderButton");
165
connect(m_minusButton, SIGNAL(clicked()), this, SLOT(minusClicked()));
167
m_slider = new QSlider(this);
168
m_slider->setObjectName("ZoomSliderSlider");
169
m_slider->setOrientation(Qt::Horizontal);
170
m_slider->setRange(MIN_VALUE, MAX_VALUE);
171
m_slider->setValue(STARTING_VALUE);
172
m_slider->setTickPosition(QSlider::TicksBelow);
173
m_slider->setTickInterval(500);
175
m_plusButton = new ZoomLabel(this);
176
m_plusButton->setImages(":/resources/images/icons/zoomSliderPlus.png", ":/resources/images/icons/zoomSliderPlusPressed.png");
177
m_plusButton->setAutoRepeat(true);
178
m_plusButton->setObjectName("ZoomSliderButton");
179
connect(m_plusButton, SIGNAL(clicked()), this, SLOT(plusClicked()));
181
connect(m_slider, SIGNAL(valueChanged(int)), this, SLOT(sliderValueChanged(int)));
182
connect(m_lineEdit, SIGNAL(textEdited(const QString &)), this, SLOT(sliderTextEdited(const QString &)));
184
//m_userStillWriting = false;
186
//m_valueBackup = editText();
187
//m_indexBackup = itemIndex(m_valueBackup);
190
void ZoomSlider::loadFactors() {
191
QFile file(":/resources/zoomfactors.txt");
192
file.open(QFile::ReadOnly);
193
QTextStream stream( &file );
195
while(!stream.atEnd()) {
196
QString line = stream.readLine();
197
if(lineNumber != 0) {
198
ZoomFactors << line.toDouble();
202
ZoomStep = line.toDouble();
210
void ZoomSlider::setValue(qreal value) {
211
QString newText = QString("%1").arg(qRound(value));
212
m_lineEdit->setText(newText);
213
sliderTextEdited(newText, false);
216
qreal ZoomSlider::value() {
217
return m_lineEdit->text().toDouble();
220
void ZoomSlider::minusClicked() {
224
void ZoomSlider::plusClicked() {
228
void ZoomSlider::step(int direction) {
230
qreal minDiff = std::numeric_limits<double>::max();
232
for (int i = 0; i < ZoomFactors.count(); i++) {
233
qreal f = ZoomFactors[i];
234
if (qAbs(f - v) < minDiff) {
235
minDiff = qAbs(f - v);
240
minIndex += direction;
244
else if (minIndex >= ZoomFactors.count()) {
245
minIndex = ZoomFactors.count() - 1;
248
setValue(ZoomFactors[minIndex]);
249
emit zoomChanged(ZoomFactors[minIndex]);
252
void ZoomSlider::sliderValueChanged(int newValue) {
253
newValue = (newValue / 10) * 10; // make it so moving the slider outputs nice rounded values
254
QString newText = QString("%1").arg(newValue);
255
if (newText.compare(m_lineEdit->text()) != 0) {
256
m_lineEdit->setText(newText);
257
emit zoomChanged(newValue);
261
void ZoomSlider::sliderTextEdited(const QString & newText) {
262
sliderTextEdited(newText, true);
265
void ZoomSlider::sliderTextEdited(const QString & newText, bool doEmit)
267
int value = newText.toInt();
268
if (m_slider->value() != value) {
269
disconnect(m_slider, SIGNAL(valueChanged(int)), this, SLOT(sliderValueChanged(int)));
270
m_slider->setValue(value);
271
connect(m_slider, SIGNAL(valueChanged(int)), this, SLOT(sliderValueChanged(int)));
272
if (doEmit) emit zoomChanged(value);
276
void ZoomSlider::zoomIn () {
280
void ZoomSlider::zoomOut () {
284
void ZoomSlider::showEvent(QShowEvent * event)
286
// can't get QHLayout to work, so shoving widgets into place here
287
// because widths aren't set at constructor time
288
QFrame::showEvent(event);
292
m_lineEdit->move(soFar, -2);
293
soFar += m_lineEdit->width();
294
m_suffix->move(soFar, 0);
295
soFar += m_suffix->width();
296
m_minusButton->move(soFar, 0);
297
soFar += m_minusButton->width();
298
m_slider->move(soFar, 0);
299
soFar += m_slider->width();
300
m_plusButton->move(soFar, 0);
1
/*******************************************************************
3
Part of the Fritzing project - http://fritzing.org
4
Copyright (c) 2007-2011 Fachhochschule Potsdam - http://fh-potsdam.de
6
Fritzing 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.
11
Fritzing 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.
16
You should have received a copy of the GNU General Public License
17
along with Fritzing. If not, see <http://www.gnu.org/licenses/>.
19
********************************************************************
22
$Author: cohen@irascible.com $:
23
$Date: 2011-07-30 21:17:22 +0200 (Sat, 30 Jul 2011) $
25
********************************************************************/
30
#include <QTextStream>
32
#include <QHBoxLayout>
37
#include "zoomslider.h"
38
#include "../debugdialog.h"
40
double ZoomSlider::ZoomStep;
41
QList<double> ZoomSlider::ZoomFactors;
43
static const int MIN_VALUE = 10;
44
static const int MAX_VALUE = 2010;
45
static const int STARTING_VALUE = 100;
46
static const int HEIGHT = 16;
47
static const int STEP = 100;
49
static int AutoRepeatDelay = 0;
50
static int AutoRepeatInterval = 0;
52
///////////////////////////////////////////////
54
ZoomLabel::ZoomLabel(QWidget * parent) : QLabel(parent)
56
if (AutoRepeatDelay == 0) {
58
AutoRepeatInterval = button.autoRepeatInterval();
59
AutoRepeatDelay = button.autoRepeatDelay();
61
m_autoRepeat = m_mouseIsDown = m_mouseIsIn = m_repeated = false;
62
m_timer.setSingleShot(false);
63
m_timer.setInterval(AutoRepeatInterval);
64
connect(&m_timer, SIGNAL(timeout()), this, SLOT(repeat()));
67
ZoomLabel::~ZoomLabel()
71
void ZoomLabel::setAutoRepeat(bool autoRepeat)
73
m_autoRepeat = autoRepeat;
76
void ZoomLabel::setImages(const QString & normal, const QString & pressed)
78
m_normal.load(normal);
79
m_pressed.load(pressed);
84
void ZoomLabel::repeat()
86
if (m_mouseIsIn && m_mouseIsDown) {
92
void ZoomLabel::mousePressEvent(QMouseEvent * event)
95
this->setMouseTracking(true);
96
this->setPixmap(m_pressed);
97
QLabel::mousePressEvent(event);
98
m_mouseIsDown = m_mouseIsIn = true;
101
m_timer.start(AutoRepeatDelay);
105
void ZoomLabel::mouseMoveEvent(QMouseEvent * event)
107
QLabel::mouseMoveEvent(event);
109
QPoint p = event->pos();
110
bool mouseIsIn = p.x() >= 0 && p.y() >= 0 && p.x() <= width() && p.y() <= height();
111
if (m_mouseIsIn && !mouseIsIn) {
113
this->setPixmap(m_normal);
116
else if (!m_mouseIsIn && mouseIsIn) {
117
this->setPixmap(m_pressed);
119
m_timer.start(AutoRepeatDelay);
124
void ZoomLabel::mouseReleaseEvent(QMouseEvent * event)
127
this->setMouseTracking(false);
128
m_mouseIsDown = false;
129
this->setPixmap(m_normal);
130
QLabel::mouseReleaseEvent(event);
131
if (!m_repeated && m_mouseIsIn) {
136
/////////////////////////////////////////////
138
ZoomSlider::ZoomSlider(QWidget * parent) : QFrame(parent)
140
// layout doesn't seem to work: the slider appears too far down in the status bar
141
// because the status bar layout is privileged for the message text
144
if (ZoomFactors.size() == 0) {
148
this->setObjectName("ZoomSliderFrame");
151
m_lineEdit = new QLineEdit(this);
152
m_lineEdit->setObjectName("ZoomSliderValue");
153
m_lineEdit->setText(QString("%1").arg(STARTING_VALUE));
154
m_lineEdit->setValidator(new QIntValidator(MIN_VALUE, MAX_VALUE, this));
155
m_lineEdit->setAttribute(Qt::WA_MacShowFocusRect, 0);
156
m_lineEdit->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
158
m_suffix = new QLabel(tr("%"), this);
159
m_suffix->setObjectName("ZoomSliderLabel");
161
m_minusButton = new ZoomLabel(this);
162
m_minusButton->setImages(":/resources/images/icons/zoomSliderMinus.png", ":/resources/images/icons/zoomSliderMinusPressed.png");
163
m_minusButton->setAutoRepeat(true);
164
m_minusButton->setObjectName("ZoomSliderButton");
165
connect(m_minusButton, SIGNAL(clicked()), this, SLOT(minusClicked()));
167
m_slider = new QSlider(this);
168
m_slider->setObjectName("ZoomSliderSlider");
169
m_slider->setOrientation(Qt::Horizontal);
170
m_slider->setRange(MIN_VALUE, MAX_VALUE);
171
m_slider->setValue(STARTING_VALUE);
172
m_slider->setTickPosition(QSlider::TicksBelow);
173
m_slider->setTickInterval(500);
175
m_plusButton = new ZoomLabel(this);
176
m_plusButton->setImages(":/resources/images/icons/zoomSliderPlus.png", ":/resources/images/icons/zoomSliderPlusPressed.png");
177
m_plusButton->setAutoRepeat(true);
178
m_plusButton->setObjectName("ZoomSliderButton");
179
connect(m_plusButton, SIGNAL(clicked()), this, SLOT(plusClicked()));
181
connect(m_slider, SIGNAL(valueChanged(int)), this, SLOT(sliderValueChanged(int)));
182
connect(m_lineEdit, SIGNAL(textEdited(const QString &)), this, SLOT(sliderTextEdited(const QString &)));
184
//m_userStillWriting = false;
186
//m_valueBackup = editText();
187
//m_indexBackup = itemIndex(m_valueBackup);
190
void ZoomSlider::loadFactors() {
191
QFile file(":/resources/zoomfactors.txt");
192
file.open(QFile::ReadOnly);
193
QTextStream stream( &file );
195
while(!stream.atEnd()) {
196
QString line = stream.readLine();
197
if(lineNumber != 0) {
198
ZoomFactors << line.toDouble();
202
ZoomStep = line.toDouble();
210
void ZoomSlider::setValue(double value) {
211
QString newText = QString("%1").arg(qRound(value));
212
m_lineEdit->setText(newText);
213
sliderTextEdited(newText, false);
216
double ZoomSlider::value() {
217
return m_lineEdit->text().toDouble();
220
void ZoomSlider::minusClicked() {
224
void ZoomSlider::plusClicked() {
228
void ZoomSlider::step(int direction) {
230
double minDiff = std::numeric_limits<double>::max();
232
for (int i = 0; i < ZoomFactors.count(); i++) {
233
double f = ZoomFactors[i];
234
if (qAbs(f - v) < minDiff) {
235
minDiff = qAbs(f - v);
240
minIndex += direction;
244
else if (minIndex >= ZoomFactors.count()) {
245
minIndex = ZoomFactors.count() - 1;
248
setValue(ZoomFactors[minIndex]);
249
emit zoomChanged(ZoomFactors[minIndex]);
252
void ZoomSlider::sliderValueChanged(int newValue) {
253
newValue = (newValue / 10) * 10; // make it so moving the slider outputs nice rounded values
254
QString newText = QString("%1").arg(newValue);
255
if (newText.compare(m_lineEdit->text()) != 0) {
256
m_lineEdit->setText(newText);
257
emit zoomChanged(newValue);
261
void ZoomSlider::sliderTextEdited(const QString & newText) {
262
sliderTextEdited(newText, true);
265
void ZoomSlider::sliderTextEdited(const QString & newText, bool doEmit)
267
int value = newText.toInt();
268
if (m_slider->value() != value) {
269
disconnect(m_slider, SIGNAL(valueChanged(int)), this, SLOT(sliderValueChanged(int)));
270
m_slider->setValue(value);
271
connect(m_slider, SIGNAL(valueChanged(int)), this, SLOT(sliderValueChanged(int)));
272
if (doEmit) emit zoomChanged(value);
276
void ZoomSlider::zoomIn () {
280
void ZoomSlider::zoomOut () {
284
void ZoomSlider::showEvent(QShowEvent * event)
286
// can't get QHLayout to work, so shoving widgets into place here
287
// because widths aren't set at constructor time
288
QFrame::showEvent(event);
292
m_lineEdit->move(soFar, -2);
293
soFar += m_lineEdit->width();
294
m_suffix->move(soFar, 0);
295
soFar += m_suffix->width();
296
m_minusButton->move(soFar, 0);
297
soFar += m_minusButton->width();
298
m_slider->move(soFar, 0);
299
soFar += m_slider->width();
300
m_plusButton->move(soFar, 0);