summaryrefslogtreecommitdiffstats
path: root/noatun-plugins/oblique/cmodule.cpp
blob: ce5caeacb900027e0ee612d5af09d490d87240ee (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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
// Copyright (c) 2003 Charles Samuels <[email protected]>
// See the file COPYING for redistribution terms.

#include "cmodule.h"
#include "oblique.h"
#include "file.h"

#include <kregexpeditorinterface.h>
#include <tdeparts/componentfactory.h>
#include <tdelocale.h>
#include <tdelistview.h>
#include <klineedit.h>
#include <kiconloader.h>
#include <kinputdialog.h>
#include <tdeconfig.h>

#include <tqgroupbox.h>
#include <tqcheckbox.h>
#include <tqlabel.h>
#include <tqvbox.h>
#include <tqhbox.h>
#include <tqfileinfo.h>
#include <tqtooltip.h>
#include <tqpushbutton.h>
#include <tqlayout.h>
#include <tqcombobox.h>
#include <tqwhatsthis.h>
#include <tqtabwidget.h>
#include <tqheader.h>

SchemaConfig::SchemaConfig(TQWidget *parent, Oblique *oblique)
	: TQWidget(parent)
{
	mOblique = oblique;
	mIgnore = true;
	mRegexpEditor=0;

	{
		TQVBoxLayout *layout = new TQVBoxLayout(this, 11, 7);
		layout->setAutoAdd(true);
		layout->setSpacing(7);
	}


	{
		TQHBox *box = new TQHBox(this);
		box->setSpacing(7);
		mSchemaList = new TQComboBox(box);
		connect(
				mSchemaList, TQT_SIGNAL(activated(const TQString&)),
				TQT_SLOT(selectSchema(const TQString&))
			);

		mAdd = new TQPushButton(BarIconSet("filenew"), 0, box);
		mAdd->setFixedWidth(mAdd->height());
		TQToolTip::add(mAdd, i18n("Create new schema"));
		connect(mAdd, TQT_SIGNAL(clicked()), TQT_SLOT(newSchema()));

		mRemove = new TQPushButton(BarIconSet("editdelete"), 0, box);
		mRemove->setFixedWidth(mRemove->height());
		TQToolTip::add(mRemove, i18n("Remove this schema"));
		connect(mRemove, TQT_SIGNAL(clicked()), TQT_SLOT(removeSchema()));

		mCopy = new TQPushButton(BarIconSet("editcopy"), 0, box);
		mCopy->setFixedWidth(mCopy->height());
		TQToolTip::add(mCopy, i18n("Copy this schema"));
		connect(mCopy, TQT_SIGNAL(clicked()), TQT_SLOT(copySchema()));
	}


	{
		TQHBox *middle = new TQHBox(this);
		middle->setSpacing(7);

		mSchemaTree = new TDEListView(middle);
		connect(
				mSchemaTree, TQT_SIGNAL(currentChanged(TQListViewItem*)),
				TQT_SLOT(setCurrent(TQListViewItem*))
			);
		connect(
				mSchemaTree, TQT_SIGNAL(moved(TQListViewItem *, TQListViewItem *, TQListViewItem *)),
				TQT_SLOT(move(TQListViewItem *, TQListViewItem *, TQListViewItem *))
			);

		mSchemaTree->setAcceptDrops(true);
		mSchemaTree->setSorting(-1);
		mSchemaTree->setDropVisualizer(true);
		mSchemaTree->setDragEnabled(true);
		mSchemaTree->setItemsMovable(true);

		mSchemaTree->addColumn(i18n("Property"));
		mSchemaTree->addColumn(i18n("Value"));
		mSchemaTree->addColumn(i18n("Presentation"));

		TQVBox *buttons = new TQVBox(middle);

		mAddSibling = new TQPushButton(BarIconSet("1rightarrow", TDEIcon::SizeSmall), "",buttons);
		mAddSibling->setFixedWidth(mAddSibling->height());
		connect(mAddSibling, TQT_SIGNAL(clicked()), TQT_SLOT(addSibling()));
		TQToolTip::add(mAddSibling, i18n("Create a new item after the selected one"));

		mAddChild = new TQPushButton(BarIconSet("2rightarrow", TDEIcon::SizeSmall), "", buttons);
		mAddChild->setFixedWidth(mAddChild->height());
		connect(mAddChild, TQT_SIGNAL(clicked()), TQT_SLOT(addChild()));
		TQToolTip::add(mAddChild, i18n("Create a new child item under the selected one"));

		mRemoveSelf = new TQPushButton(BarIconSet("filenew", TDEIcon::SizeSmall), "", buttons);
		mRemoveSelf->setFixedWidth(mRemoveSelf->height());
		connect(mRemoveSelf, TQT_SIGNAL(clicked()), TQT_SLOT(removeSelf()));
		TQToolTip::add(mRemoveSelf, i18n("Remove the selected item"));

		new TQWidget(buttons);
	}


	{
		TQVBox *side = new TQVBox(this);
		side->setSpacing(7);
		// the controllers
		{
			TQWidget *topSide = new TQWidget(side);
			TQGridLayout *grid = new TQGridLayout(topSide);

			TQLabel *label;

			label = new TQLabel(i18n("&Property:"), topSide);
			mPropertyEdit = new KLineEdit(topSide);
			label->setBuddy(mPropertyEdit);
			grid->addWidget(label, 0, 0);
			grid->addMultiCellWidget(mPropertyEdit, 0, 0, 1, 2);
			connect(mPropertyEdit, TQT_SIGNAL(textChanged(const TQString&)), TQT_SLOT(updateCurrent()));

			label = new TQLabel(i18n("&Value:"), topSide);
			mValueEdit = new KLineEdit(topSide);
			label->setBuddy(mPropertyEdit);
			grid->addWidget(label, 1, 0);
			grid->addMultiCellWidget(mValueEdit, 1, 1, 1, 1);
			connect(mValueEdit, TQT_SIGNAL(textChanged(const TQString&)), TQT_SLOT(updateCurrent()));

			TQPushButton *editRe = new TQPushButton(i18n("&Edit..."), topSide);
			grid->addWidget(editRe, 1, 2);
			connect(editRe, TQT_SIGNAL(clicked()), TQT_SLOT(editValueRegexp()));

			label = new TQLabel(i18n("Pre&sentation:"), topSide);
			mPresentationEdit = new KLineEdit(topSide);
			label->setBuddy(mPropertyEdit);
			grid->addWidget(label, 2, 0);
			grid->addMultiCellWidget(mPresentationEdit, 2, 2, 1, 2);
			connect(mPresentationEdit, TQT_SIGNAL(textChanged(const TQString&)), TQT_SLOT(updateCurrent()));
		}

		{
			TQGroupBox *groupbox = new TQGroupBox(
					3, Qt::Horizontal, i18n("Options"), side
				);

			mOptionPlayable = new TQCheckBox(i18n("Play&able"), groupbox);
			TQWhatsThis::add(mOptionPlayable, i18n("This branch represents an individual file.  If two items' presentation match, two items are created."));
			connect(mOptionPlayable, TQT_SIGNAL(toggled(bool)), TQT_SLOT(updateCurrent()));

			mOptionChildrenVisible = new TQCheckBox(i18n("&Children visible"), groupbox);
			TQWhatsThis::add(mOptionChildrenVisible, i18n("Don't create this node, this nodes children become direct children of this node's parent"));
			connect(mOptionChildrenVisible, TQT_SIGNAL(toggled(bool)), TQT_SLOT(updateCurrent()));

			mOptionAutoOpen = new TQCheckBox(i18n("Auto &open"), groupbox);
			TQWhatsThis::add(mOptionAutoOpen, i18n("This branch is marked as open immediately."));
			connect(mOptionAutoOpen, TQT_SIGNAL(toggled(bool)), TQT_SLOT(updateCurrent()));
		}

	}

}

class QueryGroupItem : public TDEListViewItem
{
	QueryGroup *mItem;

public:
	QueryGroupItem(QueryGroupItem *parent, QueryGroup *group, QueryGroupItem *after=0)
		: TDEListViewItem(parent, after)
	{
		init(group);
	}

	QueryGroupItem(TDEListView *parent, QueryGroup *group, QueryGroupItem *after=0)
		: TDEListViewItem(parent, after)
	{
		init(group);
	}

	QueryGroup *item() { return mItem; }
	const QueryGroup *item() const { return mItem; }

	void redisplay()
	{
		setText(0, item()->propertyName());
		setText(1, item()->value().pattern());
		setText(2, item()->presentation());
	}

	QueryGroupItem *parent()
		{ return static_cast<QueryGroupItem*>(TDEListViewItem::parent()); }

	TDEListView *listView()
		{ return static_cast<TDEListView*>(TDEListViewItem::listView()); }

private:
	void init(QueryGroup *group)
	{
		mItem = group;
		redisplay();

		if (group->firstChild())
			new QueryGroupItem(this, group->firstChild(), this);

		// do siblings now iff I don't already have them
		if (!nextSibling())
		{
			if (QueryGroup *after = group->nextSibling())
			{
				if (parent())
					new QueryGroupItem(parent(), after, this);
				else
					new QueryGroupItem(listView(), after, this);
			}
		}

		setOpen(true);
	}
};


void SchemaConfig::reopen()
{
	mSchemaList->clear();
	mQueries.clear();
	mSchemaTree->clear();

	TQStringList names = oblique()->schemaNames();
	TQString firstTitle;

	for (TQStringList::Iterator i(names.begin()); i != names.end(); ++i)
	{
		QueryItem qi;
		qi.title = oblique()->loadSchema(qi.query, *i);
		qi.changed = false;
		mQueries.insert(*i, qi);

		if (!mSchemaList->count())
			firstTitle = qi.title;
		mSchemaList->insertItem(qi.title);
	}
	selectSchema(firstTitle);

}

void SchemaConfig::save()
{
	for (TQMap<TQString,QueryItem>::Iterator i(mQueries.begin()); i != mQueries.end(); ++i)
	{
		TQString name = i.key();
		name = TQFileInfo(name).fileName();
		if (i.data().changed)
		{
			oblique()->saveSchema(i.data().query, name, i.data().title);
			// TODO update the trees.
		}
	}
}


TQString SchemaConfig::nameToFilename(const TQString &_name)
{
	TQString name = _name;
	name = name.replace(TQRegExp("[^a-zA-Z0-9]"), "_");
	return name;
}

void SchemaConfig::newSchema()
{
	bool ok;
	TQString str=KInputDialog::getText(
			i18n("Name of Schema"),
			i18n("Please enter the name of the new schema:"),
			"", &ok, this
		);
	if (!ok) return;

	TQString filename = nameToFilename(str);

	if (mQueries.contains(nameToFilename(filename))) return;

	QueryItem queryitem;
	queryitem.query = Query();
	queryitem.title = str;
	queryitem.changed=true;
	mSchemaList->insertItem(str);
	mQueries.insert(filename, queryitem);

	selectSchema(str);
}

void SchemaConfig::copySchema()
{
	bool ok;
	TQString str=KInputDialog::getText(
			i18n("Name of Schema"),
			i18n("Please enter the name of the new schema:"),
			"", &ok, this
		);
	if (!ok) return;

	TQString filename = nameToFilename(str);

	if (mQueries.contains(nameToFilename(filename))) return;

	QueryItem queryitem;
	queryitem.query = currentQuery()->query;
	queryitem.title = str;
	queryitem.changed=true;
	mSchemaList->insertItem(str);
	mQueries.insert(filename, queryitem);

	selectSchema(str);
}

void SchemaConfig::removeSchema()
{
	QueryItem *item = currentQuery();
	mSchemaList->removeItem(mSchemaList->currentItem());
	oblique()->removeSchema(nameToFilename(item->title));
	selectSchema(mSchemaList->currentText());
}

void SchemaConfig::selectSchema(const TQString &title)
{
	mSchemaTree->clear();
	mSchemaList->setCurrentText(title);

	mIgnore = true;
	if (QueryItem *grp = currentQuery())
	{
		if (QueryGroup *g = grp->query.firstChild())
			new QueryGroupItem(mSchemaTree, g);
	}
	mSchemaTree->setCurrentItem(mSchemaTree->firstChild());
	setCurrent(mSchemaTree->firstChild());
	mSchemaTree->setSelected(mSchemaTree->firstChild(), true);
	mIgnore=false;
}

void SchemaConfig::editValueRegexp()
{
	unless (mRegexpEditor)
	{
		mRegexpEditor =
			KParts::ComponentFactory::createInstanceFromQuery<TQDialog>(
					"KRegExpEditor/KRegExpEditor", TQString(), TQT_TQOBJECT(this)
				);
	}

	if ( mRegexpEditor )
	{
		KRegExpEditorInterface *iface =
			static_cast<KRegExpEditorInterface*>(
					mRegexpEditor->tqt_cast( "KRegExpEditorInterface")
				);

		iface->setRegExp(mValueEdit->text());
		if (mRegexpEditor->exec() == TQDialog::Accepted)
			mValueEdit->setText(iface->regExp());
	}
}

void SchemaConfig::setCurrent(TQListViewItem *_item)
{
	if (!_item) return;
	QueryGroupItem *item = static_cast<QueryGroupItem*>(_item);
	mIgnore = true;
	mPropertyEdit->setText(item->item()->propertyName());
	mValueEdit->setText(item->item()->value().pattern());
	mPresentationEdit->setText(item->item()->presentation());

	mOptionPlayable->setChecked(item->item()->option(QueryGroup::Playable));
	mOptionAutoOpen->setChecked(item->item()->option(QueryGroup::AutoOpen));
	mOptionChildrenVisible->setChecked(item->item()->option(QueryGroup::ChildrenVisible));
	mIgnore = false;
}


void SchemaConfig::updateCurrent()
{
	QueryGroupItem *item = static_cast<QueryGroupItem*>(mSchemaTree->currentItem());
	if (mIgnore || !item) return;
	setCurrentModified();

	QueryGroup *mod = item->item();

	mod->setPropertyName(mPropertyEdit->text());
	mod->setPresentation(mPresentationEdit->text());
	mod->setValue(TQRegExp(mValueEdit->text()));

	mod->setOption(QueryGroup::AutoOpen, mOptionAutoOpen->isChecked());
	mod->setOption(QueryGroup::Playable, mOptionPlayable->isChecked());
	mod->setOption(QueryGroup::ChildrenVisible, mOptionChildrenVisible->isChecked());
	item->redisplay();
}

void SchemaConfig::move(TQListViewItem *_item, TQListViewItem *, TQListViewItem *_afterNow)
{
	setCurrentModified();
	QueryGroupItem *item = static_cast<QueryGroupItem*>(_item);
	QueryGroupItem *afterNow = static_cast<QueryGroupItem*>(_afterNow);

	QueryGroup *after, *under;
	under = item->parent() ? item->parent()->item() : 0;
	after = afterNow ? afterNow->item() : 0;

	item->item()->move(&currentQuery()->query, under, after);
}


void SchemaConfig::addSibling()
{
	QueryGroupItem *item = static_cast<QueryGroupItem*>(mSchemaTree->currentItem());
	unless (item)
	{
		addChild();
		return;
	}
	setCurrentModified();

	// add it
	QueryGroup * g = new QueryGroup;
	item->item()->insertAfter(g);

	// now match the action in the tree
	QueryGroupItem *created;
	if (item->parent())
		created = new QueryGroupItem(item->parent(), g, item);
	else
		created = new QueryGroupItem(item->listView(), g, item);

	// select it so the user can edit it now
	item->listView()->setCurrentItem(created);
	item->listView()->setSelected(created, true);
}


void SchemaConfig::addChild()
{
	QueryGroupItem *item = static_cast<QueryGroupItem*>(mSchemaTree->currentItem());
	setCurrentModified();

	// add it
	QueryGroup * g = new QueryGroup;
	if (item)
		item->item()->insertUnder(g);
	else
		currentQuery()->query.setFirstChild(g);

	// now match the action in the tree
	QueryGroupItem *created;
	if (item)
		created = new QueryGroupItem(item, g);
	else
		created = new QueryGroupItem(mSchemaTree, g);

	// select it so the user can edit it now
	mSchemaTree->setCurrentItem(created);
	mSchemaTree->setSelected(created, true);
}

void SchemaConfig::removeSelf()
{
	setCurrentModified();
	QueryGroupItem *item = static_cast<QueryGroupItem*>(mSchemaTree->currentItem());
	unless (item) return;
	QueryGroup *grp = item->item();
	delete item;
	currentQuery()->query.take(grp);
	delete grp;
}

void SchemaConfig::setCurrentModified()
{
	if (QueryItem *grp = currentQuery())
	  grp->changed = true;
}

SchemaConfig::QueryItem *SchemaConfig::currentQuery()
{
	TQString title = mSchemaList->currentText();
	for (TQMap<TQString,QueryItem>::Iterator i(mQueries.begin()); i != mQueries.end(); ++i)
	{
		if (i.data().title != title) continue;
		return &i.data();
	}
	return 0;
}


SliceConfig::SliceConfig(TQWidget *parent, Oblique *oblique)
	: TQWidget(parent)
{
	mOblique = oblique;
	(new TQVBoxLayout(this, 11, 7))->setAutoAdd(true);

	{
		TQHBox *middle = new TQHBox(this);
		middle->setSpacing(7);

		mSliceList = new TDEListView(middle);
		TQWhatsThis::add(mSliceList, i18n("The list of slices.  A Slice is part of the full collection. This allows you to categorize your playlist. You can add an item to the list by right clicking on it and selecting the Slice you want it in."));
		mSliceList->addColumn("");
		mSliceList->header()->hide();

		mSliceList->setSorting(0);
		mSliceList->setItemsRenameable(true);

		TQVBox *buttons = new TQVBox(middle);

		mAdd = new TQPushButton(BarIconSet("1rightarrow", TDEIcon::SizeSmall), "",buttons);
		mAdd->setFixedWidth(mAdd->height());
		connect(mAdd, TQT_SIGNAL(clicked()), TQT_SLOT(addSibling()));
		TQToolTip::add(mAdd, i18n("Create a new item"));

		mRemove = new TQPushButton(BarIconSet("filenew", TDEIcon::SizeSmall), "", buttons);
		mRemove->setFixedWidth(mRemove->height());
		connect(mRemove, TQT_SIGNAL(clicked()), TQT_SLOT(removeSelf()));
		TQToolTip::add(mRemove, i18n("Remove the selected item"));

		new TQWidget(buttons);
	}
}


class SliceListItem : public TDEListViewItem
{
	Slice *mSlice;
public:
	SliceListItem(TDEListView *parent, Slice *slice)
		 : TDEListViewItem(parent, slice->name()), mSlice(slice)
	{
	}

	/**
	 * new item
	 **/
	SliceListItem(TDEListView *parent)
		 : TDEListViewItem(parent, i18n("New Slice")), mSlice(0)
	{
	}

	Slice *slice() { return mSlice; }
};

SliceListItem *SliceConfig::currentItem()
{
	TQListViewItem *c = mSliceList->currentItem();
	return static_cast<SliceListItem*>(c);
}


void SliceConfig::reopen()
{
	mSliceList->clear();
	mRemovedItems.clear();
	mAddedItems.clear();

	TQPtrList<Slice> slices = oblique()->base()->slices();

	for (TQPtrListIterator<Slice> i(slices); *i; ++i)
	{
		Slice *slice = *i;
		new SliceListItem(mSliceList, slice);
	}
}

void SliceConfig::save()
{
	for (
			TQValueList<Slice*>::Iterator i(mRemovedItems.begin());
			i != mRemovedItems.end();
			++i
		)
	{
		(*i)->remove();
		delete *i;
	}

	for (
			TQValueList<SliceListItem*>::Iterator i(mAddedItems.begin());
			i != mAddedItems.end();
			++i
		)
	{
		oblique()->base()->addSlice((*i)->text(0));
	}

	for (TQListViewItem *i = mSliceList->firstChild(); i; i = i->nextSibling())
	{
		SliceListItem *si = static_cast<SliceListItem*>(i);

		if (si->slice())
		{
			si->slice()->setName(si->text(0));
		}
	}

	reopen();
}


void SliceConfig::addSibling()
{
	SliceListItem *si = new SliceListItem(mSliceList);
	mAddedItems.append(si);
}

void SliceConfig::removeSelf()
{
	SliceListItem *r = currentItem();
	if (mAddedItems.contains(r))
	{
		mAddedItems.remove(r);
	}
	else
	{
		Q_ASSERT(r->slice());
		mRemovedItems.append(r->slice());
	}

	delete r;
}



Configure::Configure(Oblique *oblique)
	: CModule(i18n("Oblique"), i18n("Configure Oblique Playlist"), "", oblique)
{
	(new TQVBoxLayout(this))->setAutoAdd(true);
	tabs = new TQTabWidget(this);

	tabs->addTab(slice = new SliceConfig(tabs, oblique), i18n("Slices"));
	tabs->addTab(schema = new SchemaConfig(tabs, oblique), i18n("Schemas"));
}

void Configure::reopen()
{
	schema->reopen();
	slice->reopen();
}

void Configure::save()
{
	schema->save();
	slice->save();
}


#include "cmodule.moc"