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 #include <QModelIndex>
8 
9 #include "ui/scrspinbox.h"
10 #include "scribusdoc.h"
11 #include "guidesdelegate.h"
12 #include "units.h"
13 
14 
GuidesDelegate(QObject * parent)15 GuidesDelegate::GuidesDelegate(QObject *parent)
16 	: QItemDelegate(parent)
17 {
18 }
19 
20 // QWidget * GuidesDelegate::createEditor(QWidget *parent,
21 // 									   const QStyleOptionViewItem &/* option */,
22 // 									   const QModelIndex &/* index */) const
23 // {
24 // 	Q_ASSERT_X(m_doc != 0, "GuidesDelegate::createEditor",
25 // 			   "No reference to the doc");
26 // 	ScrSpinBox *editor = new ScrSpinBox(0, m_doc->currentPage()->height(),
27 // 										parent, m_doc->unitIndex());
28 // 	return editor;
29 // }
30 
setEditorData(QWidget * editor,const QModelIndex & index) const31 void GuidesDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
32 {
33 	double value = index.model()->data(index, Qt::EditRole).toDouble();
34 	ScrSpinBox *w = static_cast<ScrSpinBox*>(editor);
35 	w->setValue(value);
36 }
37 
setModelData(QWidget * editor,QAbstractItemModel * model,const QModelIndex & index) const38 void GuidesDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
39 {
40 	ScrSpinBox *w = static_cast<ScrSpinBox*>(editor);
41 	// When user exit widget, editor value may not be committed at this point
42 	// so we have to get value from widget text
43 	double value = w->valueFromText(w->text());
44 	model->setData(index, value, Qt::EditRole);
45 }
46 
updateEditorGeometry(QWidget * editor,const QStyleOptionViewItem & option,const QModelIndex &) const47 void GuidesDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
48 {
49 	editor->setGeometry(option.rect);
50 }
51 
setDoc(ScribusDoc * doc)52 void GuidesDelegate::setDoc(ScribusDoc * doc)
53 {
54 	m_doc = doc;
55 }
56 
57 
58 // horizontals
59 
GuidesHDelegate(QObject * parent)60 GuidesHDelegate::GuidesHDelegate(QObject *parent)
61 	: GuidesDelegate(parent)
62 {
63 }
64 
createEditor(QWidget * parent,const QStyleOptionViewItem &,const QModelIndex &) const65 QWidget * GuidesHDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &/* option */, const QModelIndex &/* index */) const
66 {
67 	Q_ASSERT_X(m_doc != nullptr, "GuidesHDelegate::createEditor", "No reference to the doc");
68 	double uix = unitGetRatioFromIndex(m_doc->unitIndex());
69 	double min = 0.0 - (m_doc->bleeds()->top() * uix);
70 	double max = (m_doc->currentPage()->height() * uix) + (m_doc->bleeds()->bottom() * uix);
71 	ScrSpinBox *editor = new ScrSpinBox(min, max, parent, m_doc->unitIndex());
72 	return editor;
73 }
74 
75 // verticals
76 
GuidesVDelegate(QObject * parent)77 GuidesVDelegate::GuidesVDelegate(QObject *parent)
78 	: GuidesDelegate(parent)
79 {
80 }
81 
createEditor(QWidget * parent,const QStyleOptionViewItem &,const QModelIndex &) const82 QWidget * GuidesVDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &/* option */, const QModelIndex &/* index */) const
83 {
84 	Q_ASSERT_X(m_doc != nullptr, "GuidesVDelegate::createEditor", "No reference to the doc");
85 	double uix = unitGetRatioFromIndex(m_doc->unitIndex());
86 	double min = 0.0 - (m_doc->bleeds()->left() * uix);
87 	double max = (m_doc->currentPage()->width() * uix) + (m_doc->bleeds()->right() * uix);
88 	ScrSpinBox *editor = new ScrSpinBox(min, max, parent, m_doc->unitIndex());
89 	return editor;
90 }
91