1 /*
2 For general Scribus (>=1.3.2) copyright and licensing information please refer
3 to the COPYING file provided with the program. Following this notice may exist
4 a copyright and/or license notice that predates the release of Scribus 1.3.2
5 for which a new license (GPL+exception) is in place.
6 */
7 
8 #include "propertywidget_pathtext.h"
9 
10 #include <QSignalBlocker>
11 
12 #include "localemgr.h"
13 #include "scribus.h"
14 #include "scribusapp.h"
15 #include "scribusdoc.h"
16 #include "selection.h"
17 
PropertyWidget_PathText(QWidget * parent)18 PropertyWidget_PathText::PropertyWidget_PathText(QWidget* parent) : QFrame(parent)
19 {
20 	setupUi(this);
21 
22 	layout()->setAlignment(Qt::AlignTop);
23 
24 	startOffset->setValues(0, 30000, 2, 0);
25 	startOffset->setSingleStep(10);
26 
27 	distFromCurve->setValues(-300, 300, 2, 0);
28 	distFromCurve->setSingleStep(10);
29 
30 	connect(ScQApp, SIGNAL(localeChanged()), this, SLOT(localeChange()));
31 
32 	languageChange();
33 }
34 
setMainWindow(ScribusMainWindow * mw)35 void PropertyWidget_PathText::setMainWindow(ScribusMainWindow* mw)
36 {
37 	m_ScMW = mw;
38 
39 	connect(m_ScMW, SIGNAL(UpdateRequest(int)), this  , SLOT(handleUpdateRequest(int)));
40 }
41 
setDoc(ScribusDoc * d)42 void PropertyWidget_PathText::setDoc(ScribusDoc *d)
43 {
44 	if(d == (ScribusDoc*) m_doc)
45 		return;
46 
47 	if (m_doc)
48 	{
49 		disconnect(m_doc->m_Selection, SIGNAL(selectionChanged()), this, SLOT(handleSelectionChanged()));
50 		disconnect(m_doc             , SIGNAL(docChanged())      , this, SLOT(handleSelectionChanged()));
51 	}
52 
53 	m_doc  = d;
54 	m_item = nullptr;
55 
56 	if (m_doc.isNull())
57 	{
58 		disconnectSignals();
59 		return;
60 	}
61 
62 	m_unitRatio = m_doc->unitRatio();
63 	m_unitIndex = m_doc->unitIndex();
64 
65 	startOffset->setMaximum( 30000 );
66 	startOffset->setMinimum( 0 );
67 	startOffset->setSingleStep(10);
68 	distFromCurve->setMaximum( 300 );
69 	distFromCurve->setMinimum( -300 );
70 	distFromCurve->setSingleStep(10);
71 
72 	connect(m_doc->m_Selection, SIGNAL(selectionChanged()), this, SLOT(handleSelectionChanged()));
73 	connect(m_doc             , SIGNAL(docChanged())      , this, SLOT(handleSelectionChanged()));
74 }
75 
setCurrentItem(PageItem * item)76 void PropertyWidget_PathText::setCurrentItem(PageItem *item)
77 {
78 	if (!m_ScMW || m_ScMW->scriptIsRunning())
79 		return;
80 	//CB We shouldn't really need to process this if our item is the same one
81 	//maybe we do if the item has been changed by scripter.. but that should probably
82 	//set some status if so.
83 	//FIXME: This won't work until when a canvas deselect happens, m_item must be nullptr.
84 	//if (m_item == i)
85 	//	return;
86 
87 	if (item && m_doc.isNull())
88 		setDoc(item->doc());
89 
90 	m_item = item;
91 
92 	disconnectSignals();
93 	configureWidgets();
94 
95 	if (m_item)
96 	{
97 		if (m_item->isPathText())
98 		{
99 			pathTextType->setCurrentIndex(m_item->textPathType);
100 			flippedPathText->setChecked(m_item->textPathFlipped);
101 			showCurveCheckBox->setChecked(m_item->PoShow);
102 			distFromCurve->showValue(m_item->BaseOffs * -1 * m_unitRatio);
103 			if (m_item->itemText.paragraphStyle(0).alignment() == 1)
104 				startOffset->setMinimum(-3000);
105 			else
106 			{
107 				if (m_item->textToFrameDistLeft() < 0)
108 					m_item->setTextToFrameDistLeft(0);
109 				startOffset->setMinimum(0);
110 			}
111 			startOffset->showValue(m_item->textToFrameDistLeft() * m_unitRatio);
112 		}
113 		connectSignals();
114 	}
115 }
116 
connectSignals()117 void PropertyWidget_PathText::connectSignals()
118 {
119 	connect(showCurveCheckBox, SIGNAL(clicked())     , this, SLOT(handlePathLine()));
120 	connect(pathTextType     , SIGNAL(activated(int)), this, SLOT(handlePathType()));
121 	connect(flippedPathText  , SIGNAL(clicked())     , this, SLOT(handlePathFlip()));
122 	connect(startOffset      , SIGNAL(valueChanged(double)), this, SLOT(handlePathDist()));
123 	connect(distFromCurve    , SIGNAL(valueChanged(double)), this, SLOT(handlePathOffs()));
124 }
125 
disconnectSignals()126 void PropertyWidget_PathText::disconnectSignals()
127 {
128 	disconnect(showCurveCheckBox, SIGNAL(clicked())     , this, SLOT(handlePathLine()));
129 	disconnect(pathTextType     , SIGNAL(activated(int)), this, SLOT(handlePathType()));
130 	disconnect(flippedPathText  , SIGNAL(clicked())     , this, SLOT(handlePathFlip()));
131 	disconnect(startOffset      , SIGNAL(valueChanged(double)), this, SLOT(handlePathDist()));
132 	disconnect(distFromCurve    , SIGNAL(valueChanged(double)), this, SLOT(handlePathOffs()));
133 }
134 
configureWidgets()135 void PropertyWidget_PathText::configureWidgets()
136 {
137 	bool enabled = false;
138 	if (m_item && m_doc)
139 	{
140 		enabled  = m_item->isPathText();
141 		enabled &= (m_doc->m_Selection->count() == 1);
142 	}
143 	setEnabled(enabled);
144 }
145 
handleSelectionChanged()146 void PropertyWidget_PathText::handleSelectionChanged()
147 {
148 	if (!m_doc || !m_ScMW || m_ScMW->scriptIsRunning())
149 		return;
150 
151 	PageItem* currItem = currentItemFromSelection();
152 	setCurrentItem(currItem);
153 	updateGeometry();
154 }
155 
handleUpdateRequest(int)156 void PropertyWidget_PathText::handleUpdateRequest(int /*updateFlags*/)
157 {
158 	// Nothing to do
159 }
160 
handlePathDist()161 void PropertyWidget_PathText::handlePathDist()
162 {
163 	if (!m_doc || !m_item || !m_ScMW || m_ScMW->scriptIsRunning())
164 		return;
165 	m_item->setTextToFrameDistLeft(startOffset->value() / m_unitRatio);
166 	m_doc->adjustItemSize(m_item);
167 	m_item->updatePolyClip();
168 	m_item->update();
169 	m_doc->regionsChanged()->update(QRect());
170 }
171 
handlePathFlip()172 void PropertyWidget_PathText::handlePathFlip()
173 {
174 	if (!m_doc || !m_item || !m_ScMW || m_ScMW->scriptIsRunning())
175 		return;
176 	m_item->textPathFlipped = flippedPathText->isChecked();
177 	m_item->updatePolyClip();
178 	m_item->update();
179 	m_doc->regionsChanged()->update(QRect());
180 }
181 
handlePathLine()182 void PropertyWidget_PathText::handlePathLine()
183 {
184 	if (!m_doc || !m_item || !m_ScMW || m_ScMW->scriptIsRunning())
185 		return;
186 	m_item->PoShow = showCurveCheckBox->isChecked();
187 	m_item->update();
188 }
189 
handlePathOffs()190 void PropertyWidget_PathText::handlePathOffs()
191 {
192 	if (!m_doc || !m_item || !m_ScMW || m_ScMW->scriptIsRunning())
193 		return;
194 	m_item->BaseOffs = -distFromCurve->value() / m_unitRatio;
195 	m_doc->adjustItemSize(m_item);
196 	m_item->updatePolyClip();
197 	m_item->update();
198 	m_doc->regionsChanged()->update(QRect());
199 }
200 
handlePathType()201 void PropertyWidget_PathText::handlePathType()
202 {
203 	if (!m_doc || !m_item || !m_ScMW || m_ScMW->scriptIsRunning())
204 		return;
205 	m_item->textPathType = pathTextType->currentIndex();
206 	m_item->update();
207 	m_doc->regionsChanged()->update(QRect());
208 }
209 
changeEvent(QEvent * e)210 void PropertyWidget_PathText::changeEvent(QEvent *e)
211 {
212 	if (e->type() == QEvent::LanguageChange)
213 	{
214 		languageChange();
215 		return;
216 	}
217 	QWidget::changeEvent(e);
218 }
219 
languageChange()220 void PropertyWidget_PathText::languageChange()
221 {
222 	QSignalBlocker pathTextTypeBlocker(pathTextType);
223 	int oldPathType = pathTextType->currentIndex();
224 	pathTextType->clear();
225 	pathTextType->addItem( tr("Default"));
226 	pathTextType->addItem( tr("Stair Step"));
227 	pathTextType->addItem( tr("Skew"));
228 	pathTextType->setCurrentIndex(oldPathType);
229 
230 	flippedPathText->setText( tr("Flip Text"));
231 	showCurveCheckBox->setText( tr("Show Curve"));
232 	pathTextTypeLabel->setText( tr("Type:"));
233 	startOffsetLabel->setText( tr("Start Offset:"));
234 	distFromCurveLabel->setText( tr("Distance from Curve:"));
235 
236 	QString ptSuffix = tr(" pt");
237 	QString unitSuffix = m_doc ? unitGetSuffixFromIndex(m_doc->unitIndex()) : ptSuffix;
238 	startOffset->setSuffix(unitSuffix);
239 	distFromCurve->setSuffix(unitSuffix);
240 }
241 
unitChange()242 void PropertyWidget_PathText::unitChange()
243 {
244 	if (!m_doc)
245 		return;
246 
247 	QSignalBlocker startOffsetBlocker(startOffset);
248 	QSignalBlocker distFromCurveBlocker(distFromCurve);
249 
250 	m_unitRatio = m_doc->unitRatio();
251 	m_unitIndex = m_doc->unitIndex();
252 
253 	startOffset->setNewUnit(m_unitIndex);
254 	distFromCurve->setNewUnit(m_unitIndex);
255 }
256 
localeChange()257 void PropertyWidget_PathText::localeChange()
258 {
259 	const QLocale& l(LocaleManager::instance().userPreferredLocale());
260 	startOffset->setLocale(l);
261 	distFromCurve->setLocale(l);
262 }
263