r23498 by jghali - Refactor DashPreview class

scribus-commit scribus-commit at lists.scribus.net
Tue Mar 10 15:45:26 UTC 2020


Author: jghali
Date: Tue Mar 10 15:45:26 2020
New Revision: 23498

URL: http://scribus.net/websvn/listing.php?repname=Scribus&sc=1&rev=23498
Log:
Refactor DashPreview class

Modified:
    trunk/Scribus/scribus/ui/dasheditor.cpp
    trunk/Scribus/scribus/ui/dasheditor.h

Modified: trunk/Scribus/scribus/ui/dasheditor.cpp
URL: http://scribus.net/websvn/diff.php?repname=Scribus&rev=23498&path=/trunk/Scribus/scribus/ui/dasheditor.cpp
==============================================================================
--- trunk/Scribus/scribus/ui/dasheditor.cpp	(original)
+++ trunk/Scribus/scribus/ui/dasheditor.cpp	Tue Mar 10 15:45:26 2020
@@ -25,7 +25,9 @@
 
 #include <QApplication>
 #include <QCursor>
+#include <QDoubleSpinBox>
 #include <QEvent>
+#include <QLabel>
 #include <QMouseEvent>
 #include <QPaintEvent>
 #include <QPainter>
@@ -46,55 +48,54 @@
 	setMinimumSize(QSize(200, 35));
 	setMaximumSize(QSize(3000, 35));
 	setMouseTracking(true);
-	Mpressed = false;
-	outside = false;
-	onlyselect = true;
-	StopM.clear();
-	ActStop = 0;
-	DashValues.clear();
-	DashValues.append(4.0);
-	DashValues.append(2.0);
+
+	m_dashValues.clear();
+	m_dashValues.append(4.0);
+	m_dashValues.append(2.0);
 } 
 
 void DashPreview::paintEvent(QPaintEvent *e)
 {
-	if (onlyselect)
-		StopM.clear();
-	int pWidth = width()-20;
+	if (m_onlySelect)
+		m_stops.clear();
+
+	int pWidth = width() - 20;
 	QImage pixm(pWidth, 10, QImage::Format_ARGB32_Premultiplied);
+
 	ScPainter *p = new ScPainter(&pixm, pWidth, 10);
 	p->clear(QColor(128, 128, 128));
 	double startX = 0.0;
 	p->setLineWidth(0);
 	p->setFillMode(1);
 	p->setBrush(Qt::black);
-	for (int a = 0; a < DashValues.count(); a++)
-	{
-		if (a % 2 == 0)
+	for (int i = 0; i < m_dashValues.count(); i++)
+	{
+		if (i % 2 == 0)
 			p->setBrush(Qt::black);
 		else
 			p->setBrush(Qt::white);
-		double w = DashValues[a] * 10;
+		double w = m_dashValues[i] * 10;
 		p->drawRect(startX, 0, w, 10);
 		startX += w;
-		if (onlyselect)
-			StopM.append(startX);
+		if (m_onlySelect)
+			m_stops.append(startX);
 	}
 	p->setPen(Qt::black, 1, Qt::SolidLine, Qt::FlatCap, Qt::MiterJoin);
 	p->setFillMode(0);
 	p->drawRect(0, 0, pWidth, 10);
 	p->end();
 	delete p;
+
 	QPainter pw;
 	pw.begin(this);
 	pw.drawImage(10, 5, pixm);
-	for (int a = 0; a < StopM.count(); ++a)
-	{
-		double center = StopM[a]+10;
+	for (int i = 0; i < m_stops.count(); ++i)
+	{
+		double center = m_stops[i] + 10;
 		pw.setPen(QPen(Qt::black, 1, Qt::SolidLine, Qt::FlatCap, Qt::MiterJoin));
-		if (ActStop == a)
-		{
-			emit currStep(DashValues[ActStop]);
+		if (m_currentStop == i)
+		{
+			emit currStep(m_dashValues[m_currentStop]);
 			pw.setBrush(Qt::red);
 		}
 		else
@@ -104,25 +105,28 @@
 		pw.drawPolygon(cr);
 	}
 	pw.end();
+
 	QFrame::paintEvent(e);
-	onlyselect = true;
+	m_onlySelect = true;
 }
 
 void DashPreview::mousePressEvent(QMouseEvent *m)
 {
 	QRect fpo;
 	m_moveTimer.start();
-	Mpressed = true;
-	ActStop = -1;
+	m_mousePressed = true;
+	m_currentStop = -1;
+
 	m->accept();
 	qApp->setOverrideCursor(QCursor(Qt::ArrowCursor));
-	for (int yg = 0; yg < StopM.count(); ++yg)
-	{
-		fpo = QRect(static_cast<int>(StopM[yg]) + 6, 16, 8, 13);
+
+	for (int i = 0; i < m_stops.count(); ++i)
+	{
+		fpo = QRect(static_cast<int>(m_stops[i]) + 6, 16, 8, 13);
 		if (fpo.contains(m->pos()))
 		{
-			ActStop = yg;
-			emit currStep(DashValues[ActStop]);
+			m_currentStop = i;
+			emit currStep(m_dashValues[m_currentStop]);
 			repaint();
 			return;
 		}
@@ -133,78 +137,79 @@
 {
 	m->accept();
 	qApp->restoreOverrideCursor();
-	if ((Mpressed) && (StopM.count() > 2) && (outside || m->y() > 30))
-	{
-		StopM.removeAt(ActStop);
-		DashValues.clear();
+	if ((m_mousePressed) && (m_stops.count() > 2) && (m_outside || m->y() > 30))
+	{
+		m_stops.removeAt(m_currentStop);
+		m_dashValues.clear();
 		double startX = 0.0;
-		for (int yg = 0; yg < StopM.count(); ++yg)
-		{
-			double w = StopM[yg] / 10.0 - startX;
-			DashValues.append(w);
+		for (int i = 0; i < m_stops.count(); ++i)
+		{
+			double w = m_stops[i] / 10.0 - startX;
+			m_dashValues.append(w);
 			startX += w;
 		}
-		ActStop = 0;
-		onlyselect = true;
-		Mpressed = false;
+		m_currentStop = 0;
+		m_onlySelect = true;
+		m_mousePressed = false;
 		repaint();
-		emit currStep(DashValues[ActStop]);
+		emit currStep(m_dashValues[m_currentStop]);
 		emit dashChanged();
 		return;
 	}
 	if ((m->y() < height()) && (m->y() > 16) && (m->x() > 9) && (m->x() < width() - 9))
 	{
-		if (ActStop != -1)
+		if (m_currentStop != -1)
 		{
 			if (m_moveTimer.elapsed() < 250)
 			{
-				Mpressed = false;
+				m_mousePressed = false;
 				return;
 			}
-			StopM[ActStop] = m->x()-10;
-		}
-		else
-		{
-			if (DashValues.count() < 10)
-				StopM.append(m->x()-10);
-			qSort(StopM.begin(), StopM.end());
-			ActStop = 0;
-			for (int yg = 0; yg < StopM.count(); ++yg)
+			m_stops[m_currentStop] = m->x() - 10;
+		}
+		else
+		{
+			if (m_dashValues.count() < 10)
+				m_stops.append(m->x() - 10);
+			qSort(m_stops.begin(), m_stops.end());
+			m_currentStop = 0;
+			for (int i = 0; i < m_stops.count(); ++i)
 			{
-				QRect fpo = QRect(static_cast<int>(StopM[yg]) + 6, 16, 8, 13);
+				QRect fpo = QRect(static_cast<int>(m_stops[i]) + 6, 16, 8, 13);
 				if (fpo.contains(m->pos()))
 				{
-					ActStop = yg;
+					m_currentStop = i;
 					break;
 				}
 			}
 		}
-		DashValues.clear();
+		m_dashValues.clear();
 		double startX = 0.0;
-		for (int yg = 0; yg < StopM.count(); ++yg)
-		{
-			double w = StopM[yg] / 10.0 - startX;
-			DashValues.append(w);
+		for (int i = 0; i < m_stops.count(); ++i)
+		{
+			double w = m_stops[i] / 10.0 - startX;
+			m_dashValues.append(w);
 			startX += w;
 		}
-		onlyselect = true;
+		m_onlySelect = true;
 		repaint();
-		emit currStep(DashValues[ActStop]);
+		emit currStep(m_dashValues[m_currentStop]);
 		emit dashChanged();
 	}
-	Mpressed = false;
+	m_mousePressed = false;
 }
 
 void DashPreview::mouseMoveEvent(QMouseEvent *m)
 {
 	m->accept();
+
 	QRect fpo;
-	if ((!Mpressed) && (m->y() < height()) && (m->y() > 16) && (m->x() > 9) && (m->x() < width()-9) && (DashValues.count() < 10))
+	if ((!m_mousePressed) && (m->y() < height()) && (m->y() > 16) && (m->x() > 9) && (m->x() < width() - 9) && (m_dashValues.count() < 10))
 	{
 		setCursor(IconManager::instance().loadCursor("AddPoint.png", 1, 1));
-		for (int yg = 0; yg < StopM.count(); ++yg)
-		{
-			fpo = QRect(static_cast<int>(StopM[yg])+6, 16, 8, 13);
+		for (int i = 0; i < m_stops.count(); ++i)
+		{
+			fpo = QRect(static_cast<int>(m_stops[i]) + 6, 16, 8, 13);
 			if (fpo.contains(m->pos()))
 			{
 				setCursor(QCursor(Qt::SizeHorCursor));
@@ -212,80 +217,80 @@
 			}
 		}
 	}
-	if ((Mpressed) && (m->y() < height()) && (m->y() > 16) && (m->x() > 9) && (m->x() < width()-9) && (ActStop != -1))
+	if ((m_mousePressed) && (m->y() < height()) && (m->y() > 16) && (m->x() > 9) && (m->x() < width() - 9) && (m_currentStop != -1))
 	{
 		qApp->changeOverrideCursor(QCursor(Qt::SizeHorCursor));
-		if (ActStop > 1)
-		{
-			if (static_cast<int>(StopM[ActStop-1]+10)+2 >= m->x())
+		if (m_currentStop > 1)
+		{
+			if (static_cast<int>(m_stops[m_currentStop - 1] + 10) + 2 >= m->x())
 				return;
 		}
-		if (ActStop < static_cast<int>(StopM.count()-2))
-		{
-			if (static_cast<int>(StopM[ActStop+1]+10)-2 < m->x())
+		if (m_currentStop < static_cast<int>(m_stops.count() - 2))
+		{
+			if (static_cast<int>(m_stops[m_currentStop + 1] + 10) - 2 < m->x())
 				return;
 		}
-		StopM[ActStop] = m->x()-10;
-		DashValues.clear();
+		m_stops[m_currentStop] = m->x() - 10;
+		m_dashValues.clear();
 		double startX = 0.0;
-		for (int yg = 0; yg < StopM.count(); ++yg)
-		{
-			double w = StopM[yg] / 10.0 - startX;
-			DashValues.append(w);
+		for (int i = 0; i < m_stops.count(); ++i)
+		{
+			double w = m_stops[i] / 10.0 - startX;
+			m_dashValues.append(w);
 			startX += w;
 		}
-		onlyselect = true;
+		m_onlySelect = true;
 		repaint();
 		startX = 0.0;
-		for (int yg = 0; yg < ActStop; ++yg)
-		{
-			startX += StopM[yg] / 10.0 - startX;
-		}
-		emit currStep(StopM[ActStop] / 10.0 - startX);
-	}
-	if ((Mpressed) && (outside || m->y() > 30) && (ActStop >= 0) && (StopM.count() > 2))
+		for (int i = 0; i < m_currentStop; ++i)
+		{
+			startX += m_stops[i] / 10.0 - startX;
+		}
+		emit currStep(m_stops[m_currentStop] / 10.0 - startX);
+	}
+	if ((m_mousePressed) && (m_outside || m->y() > 30) && (m_currentStop >= 0) && (m_stops.count() > 2))
 		qApp->changeOverrideCursor(IconManager::instance().loadCursor("DelPoint.png", 1, 1));
 }
 
 void DashPreview::leaveEvent(QEvent*)
 {
-	if (Mpressed)
-	{
-		if ((ActStop >= 0) && (StopM.count() > 2))
+	if (m_mousePressed)
+	{
+		if ((m_currentStop >= 0) && (m_stops.count() > 2))
 			qApp->changeOverrideCursor(IconManager::instance().loadCursor("DelPoint.png", 1, 1));
 		else
 			qApp->changeOverrideCursor(QCursor(Qt::ArrowCursor));
 	}
-	outside = true;
+	m_outside = true;
 }
 
 void DashPreview::enterEvent(QEvent*)
 {
-	outside = false;
+	m_outside = false;
 }
 
 void DashPreview::setActStep(double t)
 {
-	if (ActStop == -1)
+	if (m_currentStop == -1)
 		return;
-	DashValues[ActStop] = t;
-	onlyselect = true;
+	m_dashValues[m_currentStop] = t;
+	m_onlySelect = true;
 	repaint();
 	emit dashChanged();
 }
 
 void DashPreview::setDashValues(const QVector<double>& vals)
 {
-	DashValues = vals;
-	if ((ActStop >= vals.count()) || (ActStop == -1))
-		ActStop = 0;
-	onlyselect = true;
-	if (DashValues.count() != 0)
-	{
-		if ((ActStop >= vals.count()) || (ActStop == -1))
-			emit currStep(DashValues[0]);
-		else
-			emit currStep(DashValues[ActStop]);
+	m_dashValues = vals;
+	if ((m_currentStop >= vals.count()) || (m_currentStop == -1))
+		m_currentStop = 0;
+	m_onlySelect = true;
+	if (m_dashValues.count() != 0)
+	{
+		if ((m_currentStop >= vals.count()) || (m_currentStop == -1))
+			emit currStep(m_dashValues[0]);
+		else
+			emit currStep(m_dashValues[m_currentStop]);
 	}
 	update();
 }
@@ -339,11 +344,10 @@
 void DashEditor::setDashValues(QVector<double> vals, double linewidth, double offset)
 {
 	QVector<double> tmp;
-	for (int a = 0; a < vals.count(); a++)
-	{
-		tmp.append(vals[a] / linewidth);
-	}
+	for (int i = 0; i < vals.count(); i++)
+		tmp.append(vals[i] / linewidth);
 	Preview->setDashValues(tmp);
+
 	disconnect(Offset, SIGNAL(valueChanged(double)), this, SIGNAL(dashChanged()));
 	Offset->setValue(offset / linewidth);
 	connect(Offset, SIGNAL(valueChanged(double)), this, SIGNAL(dashChanged()));
@@ -351,12 +355,10 @@
 
 QVector<double> DashEditor::getDashValues(double linewidth)
 {
-	QVector<double> tmp;
-	for (int a = 0; a < Preview->DashValues.count(); a++)
-	{
-		tmp.append(Preview->DashValues[a] * linewidth);
-	}
-	return tmp;
+	auto dashValues = Preview->dashValues();
+	for (int i = 0; i < dashValues.count(); i++)
+		dashValues[i] *= linewidth;
+	return dashValues;
 }
 
 void DashEditor::changeEvent(QEvent *e)

Modified: trunk/Scribus/scribus/ui/dasheditor.h
URL: http://scribus.net/websvn/diff.php?repname=Scribus&rev=23498&path=/trunk/Scribus/scribus/ui/dasheditor.h
==============================================================================
--- trunk/Scribus/scribus/ui/dasheditor.h	(original)
+++ trunk/Scribus/scribus/ui/dasheditor.h	Tue Mar 10 15:45:26 2020
@@ -24,18 +24,18 @@
 #ifndef DASHEDITOR_H
 #define DASHEDITOR_H
 
-#include <QLabel>
+#include <QElapsedTimer>
 #include <QFrame>
-#include <QDoubleSpinBox>
+#include <QHBoxLayout>
 #include <QLayout>
 #include <QList>
+#include <QMouseEvent>
 #include <QPaintEvent>
-#include <QMouseEvent>
 #include <QVBoxLayout>
-#include <QHBoxLayout>
-#include <QTime>
 
+class QDoubleSpinBox;
 class QEvent;
+class QLabel;
 
 #include "scribusapi.h"
 #include "vgradient.h"
@@ -47,23 +47,29 @@
 public:
 	DashPreview(QWidget *pa);
 	~DashPreview() {};
-	void paintEvent(QPaintEvent *e);
-	void mousePressEvent(QMouseEvent *m);
-	void mouseReleaseEvent(QMouseEvent *);
-	void mouseMoveEvent(QMouseEvent *m);
-	void leaveEvent(QEvent*);
-	void enterEvent(QEvent*);
+
+	void paintEvent(QPaintEvent *e) override;
+	void mousePressEvent(QMouseEvent *m) override;
+	void mouseReleaseEvent(QMouseEvent *) override;
+	void mouseMoveEvent(QMouseEvent *m) override;
+	void leaveEvent(QEvent*) override;
+	void enterEvent(QEvent*) override;
+
+	const QVector<double>& dashValues() const { return m_dashValues; }
 	void setDashValues(const QVector<double>& vals);
-	QVector<double> DashValues;
-	QList<double> StopM;
-	bool Mpressed;
-	bool outside;
-	bool onlyselect;
-	int ActStop;
-	QTime m_moveTimer;
 
 public slots:
 	void setActStep(double t);
+
+private:
+	bool  m_onlySelect { true };
+	bool  m_outside { false };
+	bool  m_mousePressed { false };
+	int   m_currentStop { 0 };
+	QElapsedTimer m_moveTimer;
+
+	QVector<double> m_dashValues;
+	QList<double>   m_stops;
 
 signals:
 	void currStep(double);




More information about the scribus-commit mailing list