Welcome to mirror list, hosted at ThFree Co, Russian Federation.

AppStyleWindow.cpp « app « intern « freestyle « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d8be753b49801e3d7caa2a0c0e3c8182e2104683 (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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366

//
//  Copyright (C) : Please refer to the COPYRIGHT file distributed 
//   with this source distribution. 
//
//  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 2
//  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, write to the Free Software
//  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//
///////////////////////////////////////////////////////////////////////////////

#include <fstream>
#include <QCursor>
#include <QApplication>
#include <QFileDialog>
#include <QHeaderView>
#include <QString>
#include "AppStyleWindow.h"
#include "../stroke/Canvas.h"
#include "../stroke/StyleModule.h"
#include "Controller.h"
#include "AppInteractiveShaderWindow.h"
#include "AppConfig.h"

AppStyleWindow::AppStyleWindow(QWidget* parent /* = 0 */, const char* name /* = 0 */, Qt::WFlags fl /* = 0 */)
  : QDialog(parent, fl)
{
  // QDialog *widget = new QDialog(parent);
  setupUi(this);
	PlayList->setShowGrid(false);
	PlayList->verticalHeader()->setVisible(false);
	PlayList->horizontalHeader()->setClickable(false);
	PlayList->setSelectionBehavior(QAbstractItemView::SelectRows);
	PlayList->setSelectionMode(QAbstractItemView::SingleSelection);
	PlayList->setColumnCount(5);
  PlayList->setColumnWidth(0, 37);
  PlayList->setColumnWidth(1, width() - 98);
  PlayList->setColumnWidth(2, 37);
  PlayList->hideColumn(3);
  PlayList->hideColumn(4);
  PlayList->setRowCount(0);
  //PlayList->setsetLeftMargin(0);
	PlayList->setHorizontalHeaderLabels((QStringList() << "Disp." << "Style Modules" << "Mod."));
  _pInteractiveShaderWindow = new AppInteractiveShaderWindow(this);
  _pInteractiveShaderWindow->hide();
  QString projectDir(Config::Path::getInstance()->getProjectDir());
  _mod0_image = new QPixmap(QString::fromUtf8(":/icons/icons/mod0.png"));
  _mod1_image = new QPixmap(QString::fromUtf8(":/icons/icons/mod1.png"));
  _disp0_image = new QPixmap(QString::fromUtf8(":/icons/icons/eye0.png"));
  _disp1_image = new QPixmap(QString::fromUtf8(":/icons/icons/eye1.png"));

  connect(_pInteractiveShaderWindow, SIGNAL(save()), SLOT(fileSave()));
	// signals and slots connections
    connect( addButton, SIGNAL( clicked() ), this, SLOT( Add() ) );
    connect( removeButton, SIGNAL( clicked() ), this, SLOT( Remove() ) );
    connect( PlayList, SIGNAL( cellDoubleClicked(int,int) ), this, SLOT( Display(int,int) ) );
    connect( PlayList, SIGNAL( cellClicked(int,int) ), this, SLOT( ToggleLayer(int,int) ) );
    connect( clearButton, SIGNAL( clicked() ), this, SLOT( Clear() ) );
    connect( saveButton, SIGNAL( clicked() ), this, SLOT( SaveList() ) );
    connect( moveUpButton, SIGNAL( clicked() ), this, SLOT( Up() ) );
    connect( moveDownButton, SIGNAL( clicked() ), this, SLOT( Down() ) );
    connect( editButton, SIGNAL( clicked() ), this, SLOT( Edit() ) );
    connect( closeButton, SIGNAL( clicked() ), this, SLOT( Close() ) );
}

AppStyleWindow::~AppStyleWindow()
{
  delete _mod0_image;
  delete _mod1_image;
  delete _disp0_image;
  delete _disp1_image;
}

void AppStyleWindow::Add(const char* iFileName, bool iDisp) {
  //Add the item in the view box
  //PlayList->insertItem(fi.fileName());  
  // PlayList->insertItem(s);
  int currentRow;
  QFileInfo fi(iFileName);
  if(0 == PlayList->rowCount())
    {
      currentRow = -1;
    }
  else
    {
      currentRow = PlayList->currentRow();
    }
  PlayList->insertRow(currentRow+1);
	for(int i=0; i< PlayList->rowCount(); ++i){
		PlayList->setRowHeight(i, 20);
	}
  //PlayList->setRowHeight(currentRow + 1, 20);

	// eye item
	QTableWidgetItem * eye_item = new QTableWidgetItem;
	eye_item->setFlags(Qt::ItemIsEnabled);
	PlayList->setItem(currentRow + 1, 0, eye_item);
	// style module name item
	QTableWidgetItem * style_module_name_item = new QTableWidgetItem(fi.fileName());
	style_module_name_item->setFlags(Qt::ItemIsEnabled|Qt::ItemIsSelectable);
  PlayList->setItem(currentRow + 1, 1, style_module_name_item);
  PlayList->setItem(currentRow + 1, 3, new QTableWidgetItem(iFileName));
	// refresh item
	QTableWidgetItem * refresh_item = new QTableWidgetItem;
	refresh_item->setFlags(Qt::ItemIsEnabled);
	PlayList->setItem(currentRow + 1, 2, refresh_item);
	
	setModified(currentRow + 1, true);
	QTableWidgetItem *checkItem = new QTableWidgetItem;
  checkItem->setFlags(Qt::ItemIsUserCheckable);
  if(iDisp)
	  checkItem->setCheckState(Qt::Checked);
  else
		checkItem->setCheckState(Qt::Unchecked);
  PlayList->setItem(currentRow + 1, 4, checkItem);
  setChecked(currentRow + 1, iDisp);
	PlayList->setCurrentCell(currentRow + 1, 1);
	//PlayList->setRangeSelected(QTableWidgetSelectionRange( currentRow+1, 0, currentRow+1, 4), true);
	QString text = (PlayList->item(currentRow + 1, 3))->text();
	PlayList->takeVerticalHeaderItem(currentRow + 1);
  _pInteractiveShaderWindow->setCurrentShaderRow(currentRow + 1);
  _pInteractiveShaderWindow->DisplayShader(text);

  // Load the shader in memory and add it to the 
  // canvas list
  g_pController->InsertStyleModule(currentRow + 1, iFileName);
  g_pController->toggleLayer(currentRow + 1, iDisp);
}

void AppStyleWindow::AddList(const char* iFileName) {
  ifstream ifs(iFileName);
  if (!ifs.is_open()) {
    cerr << "Error: Cannot load this file" << endl;
    return;
  }
  QFileInfo fi(iFileName);
  char tmp_buffer[256];
  string s;
  bool disp = true;
  while (!ifs.eof()) {
    ifs.getline(tmp_buffer, 255);
    if (!tmp_buffer[0] || tmp_buffer[0] == '#')
      continue;
    if (tmp_buffer[0] == '0')
      disp = false;
    else
      disp = true;
    s = (const char*)fi.dir().path().toAscii().data();
    s += Config::DIR_SEP;
    s += tmp_buffer + 1;
    ifstream test(s.c_str(), ios::binary);
    if (!test.is_open()) {
      cerr << "Error: Cannot load \"" << tmp_buffer + 1 << "\"" << endl;
      continue;
    }
    Add(s.c_str(), disp);
  }
}

void AppStyleWindow::SaveList() {
  QString s = QFileDialog::getSaveFileName(
						this,
	  				   "Save file dialog"
					   "Choose a file",
						g_pController->getModulesDir(),
					   "Style modules lists (*." + Config::STYLE_MODULES_LIST_EXTENSION + ")");

  if (s.isEmpty())
    return;
  QFileInfo fi( s );
  QString ext = fi.suffix();
  if (ext != Config::STYLE_MODULES_LIST_EXTENSION)
    s += "." + Config::STYLE_MODULES_LIST_EXTENSION;
  ofstream ofs(s.toAscii().data(), ios::binary);
  if (!ofs.is_open()) {
    cerr << "Error: Cannot save this file" << endl;
    return;
  }
  
  QTableWidgetItem *checkItem;
	for (unsigned i = 0 ; i < PlayList->rowCount(); i++) {
    checkItem = PlayList->item(i, 4);
		ofs << ((checkItem->checkState() ==  Qt::Checked) ? '1' : '0');
    ofs << PlayList->item(i, 1)->text().toAscii().data() << endl;
  }
  g_pController->setModulesDir(fi.dir().path());
  cout << "Style modules list saved" << endl;
}

void AppStyleWindow::Add()
{
  // Load Module
  QString s = QFileDialog::getOpenFileName(this,
						"Open file dialog"
					   "Choose a file",
						g_pController->getModulesDir(),
					   "Style modules (*." + Config::STYLE_MODULE_EXTENSION + ")"
					   ";;"
					   "Style modules lists (*." + Config::STYLE_MODULES_LIST_EXTENSION + ")");

  QFileInfo fi( s );
  QString ext = fi.suffix();   // ext is taken after the last dot.

  if (ext == Config::STYLE_MODULE_EXTENSION) {
    g_pController->setModulesDir(fi.dir().path());
    Add(s.toAscii().data());
  }
  else if (ext == Config::STYLE_MODULES_LIST_EXTENSION) {
    g_pController->setModulesDir(fi.dir().path());
    AddList(s.toAscii().data());
  }
}

void AppStyleWindow::Remove()
{
  // Remove the selected item
  g_pController->RemoveStyleModule(PlayList->currentRow());
  PlayList->removeRow(PlayList->currentRow());
  _pInteractiveShaderWindow->fileClose();
}

void AppStyleWindow::Clear()
{
  g_pController->Clear();
	for (int i = PlayList->rowCount() - 1; i >= 0; i--)
    PlayList->removeRow(i);
  _pInteractiveShaderWindow->fileClose();
}

void AppStyleWindow::ExposeInteractiveShader()
{
  _pInteractiveShaderWindow->show();
  //_pInteractiveShaderWindow->Load();
}

void AppStyleWindow::setModified(unsigned row, bool mod) {
  if (mod) {
		PlayList->item(row, 2)->setIcon(QIcon(*_mod1_image));
    return;
  }
  Canvas* canvas = Canvas::getInstance();
  StyleModule* sm = canvas->getCurrentStyleModule();
  if (sm && sm->getAlwaysRefresh())
    return;
  PlayList->item(row, 2)->setIcon(QIcon(*_mod0_image));
}

void AppStyleWindow::setChecked(unsigned row, bool check) {
  if (check)
		PlayList->item(row, 0)->setIcon(QIcon(*_disp1_image));
  else
		PlayList->item(row, 0)->setIcon(QIcon(*_disp0_image));
}

void AppStyleWindow::Edit() {
	if(PlayList->rowCount() == 0)
    return;

  int currentRow = PlayList->currentRow();

  ExposeInteractiveShader();
	QString text = (PlayList->item(currentRow, 3)->text());
  _pInteractiveShaderWindow->setCurrentShaderRow(currentRow);
  _pInteractiveShaderWindow->DisplayShader(text);
}

void AppStyleWindow::Display( int row, int col ) {
  if(col != 1)
    return;

  Edit();
}

void AppStyleWindow::ToggleLayer(int row, int col)
{
  if(0 == PlayList->rowCount())
    return;
  
  if(col != 0)
    return;

	QTableWidgetItem *checkItem = PlayList->item(row, 4);
	if(checkItem->flags() != Qt::ItemIsUserCheckable)
    return;

	bool isChecked;
	if(checkItem->checkState() == Qt::Checked){
		checkItem->setCheckState(Qt::Unchecked);
		isChecked = false;
	}else{
		checkItem->setCheckState(Qt::Checked);
		isChecked = true;
	}
  g_pController->toggleLayer(row, isChecked);
  setChecked(row, isChecked);
}

void AppStyleWindow::Up() {
  int current = PlayList->currentRow();
  if (current > 0) {
    SwapShaders(current, current - 1);
    PlayList->clearSelection();

		PlayList->setRangeSelected(QTableWidgetSelectionRange( current-1, 0, current-1, 4), true);
		PlayList->setCurrentCell(current-1, 1);
    g_pController->updateCausalStyleModules(current - 1);
		current = current-1;
  }
}

void AppStyleWindow::Down() {
  int current = PlayList->currentRow();
  if (current < PlayList->rowCount() - 1) {
    SwapShaders(current, current + 1);
    PlayList->clearSelection();

    PlayList->setRangeSelected(QTableWidgetSelectionRange( current+1, 0, current+1, 4), true);
		PlayList->setCurrentCell(current+1, 1);

    g_pController->updateCausalStyleModules(current);
		current = current +1;
  }
}

void AppStyleWindow::fileSave() {
  int current = _pInteractiveShaderWindow->getCurrentShaderRow();
	QString text = (PlayList->item(current, 3)->text());
  g_pController->ReloadStyleModule(current, text.toAscii().data());
	QTableWidgetItem *checkItem = PlayList->item(current, 4);
	bool isChecked = (checkItem->checkState() == Qt::Checked) ? true : false;
  g_pController->toggleLayer(current, isChecked);
}

void AppStyleWindow::resetModified(bool iMod)
{
  for(int i=0; i < PlayList->rowCount(); i++)
  {
    setModified(i,iMod);
  }
}

void AppStyleWindow::SwapShaders(int i1, int i2) {
  g_pController->SwapStyleModules(i1, i2);
  //PlayList->swapRows(i1, i2);
	QTableWidgetItem *first_row_items[5];
	QTableWidgetItem *second_row_items[5];
	int i;
	for(i=0;i<5;++i){
		first_row_items[i] = PlayList->takeItem(i1, i);
		second_row_items[i] = PlayList->takeItem(i2, i);
	}
	for(i=0;i<5;++i){
		PlayList->setItem(i1, i, second_row_items[i]);
		PlayList->setItem(i2, i, first_row_items[i]);
	}
}