summaryrefslogtreecommitdiff
path: root/lslayoutwindow.cpp
blob: 3fe84ad15e876019a67feb8935644e65891d9500 (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
#include "lslayoutwindow.h"
#include "lsscenemodel.h"
#include "lsmaterialmodel.h"
#include "lspaneeditor.h"
#include "lsmaterialeditor.h"
#include "layoutgl/widget.h"
#include <QGridLayout>
#include <QFormLayout>
#include <QGroupBox>
#include <QScrollArea>

LSLayoutWindow::LSLayoutWindow(LYTPackageBase *pkg, const QString &layoutName, QWidget *parent) :
	QMainWindow(parent) {

	m_layoutName = layoutName;
	m_package = pkg;
	m_loadingSettings = true;

	m_tabWidget = new QTabWidget(this);
	setCentralWidget(m_tabWidget);

	QWidget *w;

	// prepare the Settings tab
	w = new QWidget(this);
	QGridLayout *sgrid = new QGridLayout(w);

	QGroupBox *sizeGroup = new QGroupBox("Layout Size", this);
	sgrid->addWidget(sizeGroup, 0, 0, 1, 2);

	QFormLayout *sizeForm = new QFormLayout(sizeGroup);

	m_widthBox = new QDoubleSpinBox(this);
	m_widthBox->setRange(0.0, 1000.0);
	m_heightBox = new QDoubleSpinBox(this);
	m_heightBox->setRange(0.0, 1000.0);

	sizeForm->addRow("Width", m_widthBox);
	sizeForm->addRow("Height", m_heightBox);

	connect(m_widthBox, SIGNAL(valueChanged(double)), SLOT(handleWidthChanged(double)));
	connect(m_heightBox, SIGNAL(valueChanged(double)), SLOT(handleHeightChanged(double)));


	QGroupBox *grpGroup = new QGroupBox("Groups", this);

	sgrid->addWidget(grpGroup, 1, 0, 1, 2);
	sgrid->setRowStretch(1, 1);

	m_tabWidget->addTab(w, "Layout");

	// prepare the Scene Graph tab
	m_sceneSplitter = new QSplitter(this);

	w = new QWidget(this);
	QGridLayout *ggrid = new QGridLayout(w);

	m_searchBox = new QLineEdit(this);
	m_searchBox->setPlaceholderText("Search panes...");

	m_clearSearchButton = new QPushButton("Clear", this);

	m_sceneGraph = new QTreeView(this);
	m_sceneGraph->setHeaderHidden(true);
	m_sceneSearchList = new QListView(this);
	m_sceneListSwitcher = new QStackedLayout;
	m_sceneListSwitcher->addWidget(m_sceneGraph);
	m_sceneListSwitcher->addWidget(m_sceneSearchList);

	ggrid->addWidget(m_searchBox, 0, 0, 1, 1);
	ggrid->addWidget(m_clearSearchButton, 0, 1, 1, 1);
	ggrid->addLayout(m_sceneListSwitcher, 1, 0, 1, 2);

	QWidget *switcherWidget = new QWidget(this);
	m_paneEditorSwitcher = new QStackedLayout(switcherWidget);

	m_paneEditor = new LSPaneEditor(switcherWidget);

	m_paneEditorSwitcher->addWidget(new QLabel("Choose a pane to edit from the left.", switcherWidget));
	m_paneEditorSwitcher->addWidget(m_paneEditor);

	m_sceneSplitter->addWidget(w);
	m_sceneSplitter->addWidget(switcherWidget);
	m_sceneSplitter->setCollapsible(1, false);

	m_tabWidget->addTab(m_sceneSplitter, "Scene Graph");


	// prepare the materials tab
	m_materialSplitter = new QSplitter(this);

	w = new QWidget(this);
	QGridLayout *mgrid = new QGridLayout(w);

	m_addMaterialButton = new QPushButton("Add", w);
	m_removeMaterialButton = new QPushButton("Remove", w);
	m_materialList = new QListView(w);

	mgrid->addWidget(m_materialList, 0, 0, 1, 2);
	mgrid->addWidget(m_addMaterialButton, 1, 0, 1, 1);
	mgrid->addWidget(m_removeMaterialButton, 1, 1, 1, 1);

	QScrollArea *matScroller = new QScrollArea(m_materialSplitter);
	m_materialEditor = new LSMaterialEditor(matScroller);

	m_materialSplitter->addWidget(w);
	m_materialSplitter->addWidget(matScroller);
	m_materialSplitter->setCollapsible(1, false);

	matScroller->setWidget(m_materialEditor);

	m_tabWidget->addTab(m_materialSplitter, "Materials");


	// get the resource
	m_layout = new LYTLayout(*m_package, m_layoutName);

	m_widthBox->setValue(m_layout->width);
	m_heightBox->setValue(m_layout->height);

	LSSceneModel *scnModel = new LSSceneModel(m_layout, true, this);

	m_sceneGraph->setSelectionMode(QAbstractItemView::ExtendedSelection);
	m_sceneGraph->setDragEnabled(true);
	m_sceneGraph->setAcceptDrops(true);
	m_sceneGraph->setDropIndicatorShown(true);
	m_sceneGraph->setModel(scnModel);
	m_sceneGraph->expandAll();
	connect(m_sceneGraph->selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)), SLOT(selectedPaneChanged(QModelIndex,QModelIndex)));

	LSMaterialModel *matModel = new LSMaterialModel(&m_layout->materials, this);

	m_materialList->setSelectionMode(QAbstractItemView::SingleSelection);
	m_materialList->setModel(matModel);
	connect(m_materialList->selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)), SLOT(selectedMaterialChanged(QModelIndex,QModelIndex)));

	setWindowTitle(m_layoutName);


	// finally make the widget
	m_renderer = new LGLWidget();
	m_renderer->setLayout(m_layout);
	m_renderer->setWindowTitle(QString("Preview: %1").arg(m_layoutName));
	m_renderer->setWindowFlags(
				Qt::CustomizeWindowHint |
				Qt::WindowTitleHint |
				Qt::WindowMinimizeButtonHint);
	m_renderer->show();

	connect(m_paneEditor, SIGNAL(mustRedrawLayout()), m_renderer, SLOT(updateGL()));
	connect(scnModel, SIGNAL(paneVisibilityChanged()), m_renderer, SLOT(updateGL()));

	// clean up here
	setAttribute(Qt::WA_DeleteOnClose);

	m_loadingSettings = false;
}

LSLayoutWindow::~LSLayoutWindow() {
	m_renderer->close();
	delete m_renderer;
	delete m_layout;
}


void LSLayoutWindow::selectedPaneChanged(const QModelIndex &current, const QModelIndex &previous) {
	(void)previous;
	LYTPane *pane = (LYTPane*)current.internalPointer();

	m_paneEditor->setPane(pane);
	m_paneEditorSwitcher->setCurrentIndex(1);
}

void LSLayoutWindow::selectedMaterialChanged(const QModelIndex &current, const QModelIndex &previous) {
	(void)previous;
	LYTMaterial *mat = m_layout->materials.getMaterialByIndex(current.row());

	m_materialEditor->setMaterial(mat);
}


void LSLayoutWindow::handleWidthChanged(double v) {
	m_layout->width = v;
	if (!m_loadingSettings)
		m_renderer->updateGL();
}

void LSLayoutWindow::handleHeightChanged(double v) {
	m_layout->height = v;
	if (!m_loadingSettings)
		m_renderer->updateGL();
}