summaryrefslogtreecommitdiffstats
path: root/scribus/guidesmodel.cpp
blob: 0655feba9a37620b2c81166af5b0a43c86f56c6b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
For general Scribus (>=1.3.2) copyright and licensing information please refer
to the COPYING file provided with the program. Following this notice may exist
a copyright and/or license notice that predates the release of Scribus 1.3.2
for which a new license (GPL+exception) is in place.
*/
#include <QLocale>

#include "guidesmodel.h"
#include "units.h"


GuidesModel::GuidesModel(QObject * /*parent*/)
	: QAbstractTableModel(),
		m_docUnitIndex(0),
		m_docUnitDecimals(0)
{

	// debug
// 	m_values << 1.0 << 10.1 << 6.3 << 4.1;
// 	qSort(m_values);
}


GuidesModel::~GuidesModel()
{
}

int GuidesModel::rowCount(const QModelIndex & /*parent*/) const
{
	return m_values.count();
}

int GuidesModel::columnCount(const QModelIndex & /*parent*/) const
{
	// for future RFEs - color per guide etc.
	return 1;
}

QVariant GuidesModel::data(const QModelIndex & index, int role) const
{
	if (!index.isValid())
		return QVariant();
	// DisplayRole and EditRole *must* be splitted. There is rounding
	// in pts2value(), toString() sequence. It disallows to compare
	// these values with m_data list.
	// Call it with EditRole when you need exact value.
	if (role == Qt::DisplayRole)
	{
		QLocale l;
		return QVariant(l.toString(pts2value(m_values.at(index.row()),
											  m_docUnitIndex), 'f',
											  m_docUnitDecimals)
						);
	}
	if (role == Qt::EditRole)
		return pts2value(m_values.at(index.row()), m_docUnitIndex);

	if (role == Qt::BackgroundColorRole && m_values.at(index.row()) == 0.0)
		return QVariant(Qt::red);
	return QVariant();
}

bool GuidesModel::setData(const QModelIndex & index, const QVariant & value, int role)
{
	if (role != Qt::EditRole || !index.isValid())
		return false;
	bool ok;
	double newVal = value.toDouble(&ok);
	if (!ok)
		return false;
	m_values[index.row()] = value2pts(newVal, m_docUnitIndex);
	qSort(m_values);
	emit dataChanged(index, index);
	emit valueChanged();
	return true;
}

Qt::ItemFlags GuidesModel::flags(const QModelIndex & /*index*/) const
{
	return Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsSelectable;
}

QVariant GuidesModel::headerData(int /*section*/, Qt::Orientation orientation, int /*role*/) const
{
	if (orientation == Qt::Horizontal)
		return "Value";
	return "";
}

void GuidesModel::removeValues(const Guides & v)
{
	foreach(double i, v)
		m_values.removeAll(value2pts(i, m_docUnitIndex));
	reset();
}

// bool GuidesModel::removeRows(int row, int count, const QModelIndex & parent)
// {
// 	if (count <= 0 || row < 0 || (row + count - 1) > m_values.count())
// 		return false;
// 
// 	beginRemoveRows(parent, row, row + count - 1);
// 	for (int i = 0; i < count; ++i)
// 		m_values.removeAt(row);
// 	endRemoveRows();
// 	return true;
// }

// bool GuidesModel::insertRows( int row, int count, const QModelIndex & parent)
// {
// 	beginInsertRows(parent, row, row + count - 1);
// 	for (int i = 0; i < count; ++i)
// 		m_values.insert(row + count, 0.0);
// 	endInsertRows();
// 	return true;
// }

void GuidesModel::insertRow()
{
// 	insertRows(rowCount(), 1);
	if (m_values.contains(0.0))
		return;
	m_values.append(0.0);
	qSort(m_values);
	reset();
}

void GuidesModel::setValues(Guides values)
{
	m_values = values;
	qSort(m_values);
	reset();
}

Guides GuidesModel::values()
{
	return m_values;
}

void GuidesModel::unitChange(int docUnitIndex, int docUnitDecimals)
{
	m_docUnitIndex = docUnitIndex;
	m_docUnitDecimals = docUnitDecimals;
	reset();
}

#if 0
// debug
#include <QtDebug>
void GuidesModel::printValues()
{
	qDebug() << "GuidesModel dump start";
	for (int i = 0; i < m_values.count(); ++i)
		qDebug() << "GuidesModel dump: " << m_values[i];
	qDebug() << "GuidesModel end of dump";
}
#endif