summaryrefslogtreecommitdiff
path: root/src/gui/stylepalette.cpp
blob: 15ed4c2ecef10c69570a169aad3639a462105036 (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
/*
    TikZiT - a GUI diagram editor for TikZ
    Copyright (C) 2018 Aleks Kissinger

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/

#include "stylepalette.h"
#include "ui_stylepalette.h"
#include "tikzit.h"

#include <QDebug>
#include <QIcon>
#include <QSize>
#include <QSettings>
#include <QPainter>
#include <QPixmap>
#include <QPainterPath>
#include <QMessageBox>

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

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


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

    reloadStyles();

    connect(ui->styleListView, SIGNAL(doubleClicked(const QModelIndex &)), this, SLOT( nodeStyleDoubleClicked(const QModelIndex&)) );
	connect(ui->edgeStyleListView, SIGNAL(doubleClicked(const QModelIndex &)), this, SLOT(edgeStyleDoubleClicked(const QModelIndex&)));
}

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

void StylePalette::reloadStyles()
{
    QString f = tikzit->styleFile();
    ui->styleFile->setText(f);

    ui->styleListView->setModel(tikzit->styles()->nodeStyles());
    ui->edgeStyleListView->setModel(tikzit->styles()->edgeStyles());

    QString cat = ui->currentCategory->currentText();
    ui->currentCategory->clear();

	// TODO: styleFile() should return invalid string if no style file loaded
    if (f != "[no styles]") {
		ui->currentCategory->addItems(tikzit->styles()->categories());
		ui->currentCategory->setCurrentText(cat);
	}
}

void StylePalette::changeNodeStyle(int increment)
{
    QModelIndexList i = ui->styleListView->selectionModel()->selectedIndexes();
    int row = 0;
    if (!i.isEmpty()) {
        int row = (i[0].row()+increment)% tikzit->styles()->nodeStyles()->numInCategory();
        if (row < 0) row += tikzit->styles()->nodeStyles()->numInCategory();
    }

    //QModelIndex i1 = ui->styleListView->rootIndex().child(row, 0);
    QModelIndex i1 =tikzit->styles()->nodeStyles()->index(row,0);
    ui->styleListView->selectionModel()->select(i1, QItemSelectionModel::ClearAndSelect);
    ui->styleListView->scrollTo(i1);
}

void StylePalette::nextNodeStyle()
{
    changeNodeStyle(1);
}

void StylePalette::previousNodeStyle()
{
    changeNodeStyle(-1);
}

QString StylePalette::activeNodeStyleName()
{
    const QModelIndexList i = ui->styleListView->selectionModel()->selectedIndexes();

    if (i.isEmpty()) {
        return "none";
    } else {
        return i[0].data().toString();
    }
}

QString StylePalette::activeEdgeStyleName()
{
	const QModelIndexList i = ui->edgeStyleListView->selectionModel()->selectedIndexes();

	if (i.isEmpty()) {
		return "none";
	} else {
		return i[0].data().toString();
	}
}

void StylePalette::nodeStyleDoubleClicked(const QModelIndex &)
{
    tikzit->activeWindow()->tikzScene()->applyActiveStyleToNodes();
}

void StylePalette::edgeStyleDoubleClicked(const QModelIndex &)
{
    tikzit->activeWindow()->tikzScene()->applyActiveStyleToEdges();
}

void StylePalette::on_buttonNewTikzstyles_clicked()
{
    tikzit->newTikzStyles();
}

void StylePalette::on_buttonOpenTikzstyles_clicked()
{
    tikzit->openTikzStyles();
}

void StylePalette::on_buttonEditTikzstyles_clicked()
{
    if (tikzit->styleFile() != "[no styles]") {
        tikzit->showStyleEditor();
    } else {
        QMessageBox::warning(0,
            "No style file",
            "You cannot edit styles until a style file is loaded. Either create a new style file or load an existing one.");
    }
}

void StylePalette::on_buttonRefreshTikzstyles_clicked()
{
    QSettings settings("tikzit", "tikzit");
    QString path = settings.value("previous-tikzstyles-file").toString();
    if (!path.isEmpty()) tikzit->loadStyles(path);
}

void StylePalette::on_currentCategory_currentTextChanged(const QString &cat)
{
    //tikzit->styles()->refreshModels(_nodeModel, _edgeModel, cat);
    tikzit->styles()->nodeStyles()->setCategory(cat);
}

//void StylePalette::on_buttonApplyNodeStyle_clicked()
//{
//    if (tikzit->activeWindow() != 0) tikzit->activeWindow()->tikzScene()->applyActiveStyleToNodes();
//}


void StylePalette::resizeEvent(QResizeEvent *event)
{
    QDockWidget::resizeEvent(event);
    ui->styleListView->setGridSize(QSize(48,48));
    ui->edgeStyleListView->setGridSize(QSize(48,48));
}