summaryrefslogtreecommitdiff
path: root/src/gui/styleeditor.cpp
blob: 9081873dad07331f1b24846d599ec2232ac1a7ee (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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#include <QColorDialog>
#include <QDebug>
#include <QMessageBox>

#include "tikzit.h"
#include "styleeditor.h"
#include "ui_styleeditor.h"

StyleEditor::StyleEditor(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::StyleEditor)
{
    ui->setupUi(this);

    setColor(ui->fillColor, QColor(Qt::white));
    setColor(ui->drawColor, QColor(Qt::black));
    setColor(ui->tikzitFillColor, QColor(Qt::white));
    setColor(ui->tikzitDrawColor, QColor(Qt::black));

    _styles = 0;

    _nodeModel = new QStandardItemModel(this);
    _edgeModel = new QStandardItemModel(this);

    ui->styleListView->setModel(_nodeModel);
    ui->styleListView->setViewMode(QListView::IconMode);
    ui->styleListView->setMovement(QListView::Static);
    ui->styleListView->setGridSize(QSize(48,48));


    ui->edgeStyleListView->setModel(_edgeModel);
    ui->edgeStyleListView->setViewMode(QListView::IconMode);
    ui->edgeStyleListView->setMovement(QListView::Static);
    ui->edgeStyleListView->setGridSize(QSize(48,48));

    // setup the color dialog to display only the named colors that tikzit/xcolor knows
    // about as "standard colors".
    for (int i = 0; i < 48; ++i) {
        QColorDialog::setStandardColor(i, QColor(Qt::white));
    }

    // grayscale in column 1
    int pos = 0;
    for (int i=0; i < 5; ++i) {
        QColorDialog::setStandardColor(pos, tikzit->colorByIndex(i));
        pos += 1;
    }

    // rainbow in column 2
    pos = 6;
    for (int i=5; i < 11; ++i) {
        QColorDialog::setStandardColor(pos, tikzit->colorByIndex(i));
        pos += 1;
    }

    // brown/green/teal spectrum in column 3
    pos = 12;
    for (int i=11; i < 16; ++i) {
        QColorDialog::setStandardColor(pos, tikzit->colorByIndex(i));
        pos += 1;
    }

    // pinks in column 4
    pos = 18;
    for (int i=16; i < 19; ++i) {
        QColorDialog::setStandardColor(pos, tikzit->colorByIndex(i));
        pos += 1;
    }

    _activeNodeStyle = 0;
    _activeEdgeStyle = 0;
    updateFields();
}

StyleEditor::~StyleEditor()
{
    delete ui;
}

void StyleEditor::open() {
    if (_styles != 0) delete _styles;
    _styles = new TikzStyles;
    if (_styles->loadStyles(tikzit->styleFilePath())) {
        _styles->refreshModels(_nodeModel, _edgeModel);
        show();
    } else {
        QMessageBox::warning(0,
            "Bad style file.",
            "Bad style file: '" + tikzit->styleFile() + "'. Check that the file exists and is properly formatted.");
    }
}

void StyleEditor::updateFields()
{
    ui->name->setEnabled(false);
    ui->category->setEnabled(false);
    ui->fillColor->setEnabled(false);
    ui->drawColor->setEnabled(false);
    ui->tikzitFillColor->setEnabled(false);
    ui->tikzitDrawColor->setEnabled(false);
    ui->hasTikzitFillColor->setEnabled(false);
    ui->hasTikzitDrawColor->setEnabled(false);
    ui->shape->setEnabled(false);
    ui->hasTikzitShape->setEnabled(false);
    ui->tikzitShape->setEnabled(false);
    ui->leftArrow->setEnabled(false);
    ui->rightArrow->setEnabled(false);
    ui->properties->setEnabled(false);

    if (_activeNodeStyle != 0) {
        ui->name->setEnabled(true);
        ui->name->setText(_activeNodeStyle->name());

        ui->category->setEnabled(true);
        // TODO

        // passing 'false' to these methods prevents 'tikzit foo' from overriding property 'foo'
        QColor realFill = _activeNodeStyle->fillColor(false);
        QColor fill = _activeNodeStyle->fillColor();
        bool fillOverride = realFill != fill;
        QColor realDraw = _activeNodeStyle->strokeColor(false);
        QColor draw = _activeNodeStyle->strokeColor();
        bool drawOverride = realDraw != draw;

        ui->fillColor->setEnabled(true);
        setColor(ui->fillColor, realFill);

        ui->drawColor->setEnabled(true);
        setColor(ui->drawColor, realDraw);


        ui->hasTikzitFillColor->setEnabled(true);
        ui->hasTikzitFillColor->setChecked(fillOverride);

        ui->tikzitFillColor->setEnabled(fillOverride);
        setColor(ui->tikzitFillColor, fill);

        ui->hasTikzitDrawColor->setEnabled(true);
        ui->hasTikzitDrawColor->setChecked(drawOverride);

        ui->tikzitDrawColor->setEnabled(drawOverride);
        setColor(ui->tikzitDrawColor, draw);

        // TODO
        ui->shape->setEnabled(true);
        ui->hasTikzitShape->setEnabled(true);
        ui->tikzitShape->setEnabled(true);
        ui->properties->setEnabled(true);
    } else if (_activeEdgeStyle != 0) {
        ui->name->setEnabled(true);
        ui->name->setText(_activeEdgeStyle->name());

        ui->category->setEnabled(true);
        // TODO


        setColor(ui->fillColor, QColor(Qt::gray));
        setColor(ui->tikzitFillColor, QColor(Qt::gray));
        ui->hasTikzitFillColor->setChecked(false);


        // passing 'false' to these methods prevents 'tikzit foo' from overriding property 'foo'
        QColor realDraw = _activeEdgeStyle->strokeColor(false);
        QColor draw = _activeEdgeStyle->strokeColor();
        bool drawOverride = realDraw != draw;

        ui->drawColor->setEnabled(true);
        setColor(ui->drawColor, realDraw);

        ui->hasTikzitDrawColor->setEnabled(true);
        ui->hasTikzitDrawColor->setChecked(drawOverride);

        ui->tikzitDrawColor->setEnabled(drawOverride);
        setColor(ui->tikzitDrawColor, draw);

        // TODO
        ui->leftArrow->setEnabled(true);
        ui->rightArrow->setEnabled(true);
        ui->properties->setEnabled(true);
    } else {
        setColor(ui->fillColor, QColor(Qt::gray));
        setColor(ui->drawColor, QColor(Qt::gray));
        setColor(ui->tikzitDrawColor, QColor(Qt::gray));
        setColor(ui->tikzitFillColor, QColor(Qt::gray));
    }
}

void StyleEditor::on_fillColor_clicked()
{
    updateColor(ui->fillColor, "Fill Color", "fill");
}

void StyleEditor::on_drawColor_clicked()
{
    updateColor(ui->drawColor, "Draw Color", "draw");
}

void StyleEditor::on_tikzitFillColor_clicked()
{
    updateColor(ui->tikzitFillColor, "TikZiT Fill Color", "tikzit fill");
}

void StyleEditor::on_tikzitDrawColor_clicked()
{
    updateColor(ui->tikzitDrawColor, "TikZiT Draw Color", "tikzit draw");
}

void StyleEditor::on_styleListView_clicked()
{
    _activeNodeStyle = 0;
    _activeEdgeStyle = 0;
    const QModelIndexList i = ui->styleListView->selectionModel()->selectedIndexes();
    QString sty;
    if (!i.isEmpty()) {
        _activeItem = _nodeModel->itemFromIndex(i[0]);
        sty = _activeItem->text();
        if (sty != "none")
            _activeNodeStyle = tikzit->styles()->nodeStyle(sty);
    }
    updateFields();
}

void StyleEditor::on_edgeStyleListView_clicked()
{
    _activeNodeStyle = 0;
    _activeEdgeStyle = 0;
    const QModelIndexList i = ui->edgeStyleListView->selectionModel()->selectedIndexes();
    QString sty;
    if (!i.isEmpty()) {
        _activeItem = _edgeModel->itemFromIndex(i[0]);
        sty = _activeItem->text();
        if (sty != "none")
            _activeEdgeStyle = tikzit->styles()->edgeStyle(sty);
    }
    updateFields();
}

void StyleEditor::on_name_editingFinished()
{
    Style *s;
    if (_activeNodeStyle != 0) s = _activeNodeStyle;
    else if (_activeEdgeStyle != 0) s = _activeEdgeStyle;
    else return;

    s->setName(ui->name->text());
    _activeItem->setText(ui->name->text());
    qDebug("got here");
}

void StyleEditor::setColor(QPushButton *btn, QColor col)
{
    QPalette pal = btn->palette();
    pal.setColor(QPalette::Button, col);
    btn->setPalette(pal);
    btn->update();
}

QColor StyleEditor::color(QPushButton *btn)
{
    QPalette pal = btn->palette();
    return pal.color(QPalette::Button);
}

void StyleEditor::updateColor(QPushButton *btn, QString name, QString propName)
{
    QColor col = QColorDialog::getColor(
                color(btn),
                this,
                name,
                QColorDialog::DontUseNativeDialog);
    if (col.isValid()) {
        setColor(btn, col);
        if (_activeNodeStyle != 0) {
            _activeNodeStyle->data()->setProperty(propName, tikzit->nameForColor(col));
            _activeItem->setIcon(_activeNodeStyle->icon());
        } else if (_activeEdgeStyle != 0) {
            _activeEdgeStyle->data()->setProperty(propName, tikzit->nameForColor(col));
            _activeItem->setIcon(_activeEdgeStyle->icon());
        }
    }
}