summaryrefslogtreecommitdiffstats
path: root/seriestreewidget.cpp
blob: 816a293a39ad927fdcf0e6bb9917ab70d58356c3 (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
/*
  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.
*/

#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QPushButton>
#include <QLineEdit>
#include <QLabel>
#include <QSortFilterProxyModel>
#include <QRegExp>
#include <QContextMenuEvent>
#include <QMenu>
#include <QAction>

#include "seriestreewidget.h"
#include "smubermodel.h"
#include "smtreemodel.h"
#include "smubermodelsingleton.h"
#include "seriestreemodel.h"

SeriesTreeWidget::SeriesTreeWidget(QWidget *parent) : QWidget(parent){
	//filter bar
	QLabel *l1 = new QLabel(tr("&Filter:"));
	mFilterEdit = new QLineEdit;
	l1->setBuddy(mFilterEdit);
	mFilter = new QPushButton(tr("Filter"));
	QHBoxLayout *filterLayout = new QHBoxLayout;
	connect(mFilter, SIGNAL(clicked()), this, SLOT(filter()));
	connect(mFilterEdit, SIGNAL(returnPressed()), this, SLOT(filter()));
	filterLayout->addWidget(l1);
	filterLayout->addWidget(mFilterEdit);
	filterLayout->addWidget(mFilter);

	//the view
	mView = new SeriesTreeView;
	mProxy = new QSortFilterProxyModel;
	mModel = SmUberModelSingleton::instance()->seriesModel();
	mProxy->setSourceModel(mModel);
	mView->setModel(mProxy);
	mView->setSortingEnabled(true);
	for(int i = 1; i < 5 ;++i){
		mView->setColumnHidden(i, true);
	}
	mView->resizeColumnToContents(0);
	connect(mModel, SIGNAL(needResort()), this, SLOT(resort()));

	//layout
	QVBoxLayout *mainLayout = new QVBoxLayout;
	mainLayout->addLayout(filterLayout);
	mainLayout->addWidget(mView);
	setLayout(mainLayout);

}

void SeriesTreeWidget::newSeries(){
	QList<QVariant> data;
	data << tr("<New series>") << QVariant() << QVariant() << QVariant() << SeriesTreeModel::NewSeries;
	if(mModel->addRow(data, QModelIndex())){
		QModelIndex newRow = mModel->index(mModel->rowCount(QModelIndex()) - 1, 0, QModelIndex());
		if(newRow.isValid()){
			QModelIndex proxyIndex = mProxy->mapFromSource(newRow);
			mView->selectionModel()->setCurrentIndex(proxyIndex, QItemSelectionModel::ClearAndSelect);
			mView->edit(proxyIndex);
		}
	}
}

void SeriesTreeWidget::filter(){
	mProxy->setFilterRegExp(mFilterEdit->text());
}

void SeriesTreeWidget::resort(){
	mView->sortByColumn(0, mProxy->sortOrder());
	mView->scrollTo(mView->selectionModel()->currentIndex(), QAbstractItemView::PositionAtCenter);
}

SeriesTreeView::SeriesTreeView(QWidget *parent) : QTreeView(parent) {}

void SeriesTreeView::contextMenuEvent(QContextMenuEvent *e){
	QMenu contextMenu(this);
	foreach(QAction *a, actions()){
		contextMenu.addAction(a);
	}
	contextMenu.exec(e->globalPos());
}