File: taskeditor.cpp

package info (click to toggle)
vym 2.6.11-3
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye, buster, sid, trixie
  • size: 12,628 kB
  • sloc: cpp: 34,052; ruby: 846; xml: 278; sh: 187; makefile: 28
file content (286 lines) | stat: -rw-r--r-- 8,491 bytes parent folder | download
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
#include "taskeditor.h"

#include <QAbstractTableModel>
#include <QAction>
#include <QDebug>
#include <QHeaderView>
#include <QMenu>
#include <QSortFilterProxyModel>
#include <QTextEdit>
#include <QToolBar>
#include <QVBoxLayout>

#include "branchitem.h"
#include "mainwindow.h"
#include "task.h"
#include "taskmodel.h"
#include "vymmodel.h"

extern Main *mainWindow;
extern Settings settings;
extern QMenu* taskContextMenu;
extern TaskModel* taskModel;

TaskEditor::TaskEditor(QWidget *)
{
    // Creat Table view
    view = new QTableView; 

    QVBoxLayout* mainLayout = new QVBoxLayout;

    QToolBar *tb=new QToolBar ("TaskEditor filters");
    tb->setToolButtonStyle (Qt::ToolButtonTextBesideIcon);
    mainLayout->addWidget (tb);

    // Original icon from KDE: /usr/share/icons/oxygen/16x16/actions/view-filter.png

    QIcon icon=QIcon (":/view-filter.png");
    QAction *a = new QAction(icon,  tr( "Current map","TaskEditor" ),this );
    a->setCheckable(true);
    a->setChecked  (settings.value("/taskeditor/filterMap", false).toBool());
    tb->addAction (a);
    connect( a, SIGNAL( triggered() ), this, SLOT(toggleFilterMap() ) );
    actionToggleFilterMap = a;

    a = new QAction(icon,  tr( "Active tasks","TaskEditor" ),this );
    a->setCheckable(true);
    a->setChecked  (settings.value("/taskeditor/filterActive", false).toBool());
    tb->addAction (a);
    connect( a, SIGNAL( triggered() ), this, SLOT(toggleFilterActive() ) );
    actionToggleFilterActive = a;

    a = new QAction(icon,  tr( "New tasks","TaskEditor" ),this );
    a->setCheckable(true);
    a->setChecked  (settings.value("/taskeditor/filterNew", false).toBool());
    tb->addAction (a);
    connect( a, SIGNAL( triggered() ), this, SLOT(toggleFilterNew() ) );
    actionToggleFilterNew = a;

    a = new QAction(icon,  "Flag", this); //tr( "Flags","TaskEditor" ),this );   // FIXME-1 add translation
    a->setCheckable(true);
    a->setChecked  (settings.value("/taskeditor/filterNew", false).toBool());
    if (settings.value( "/mainwindow/showTestMenu",false).toBool())
        tb->addAction (a);
    connect( a, SIGNAL( triggered() ), this, SLOT(toggleFilterFlags() ) );
    actionToggleFilterFlags = a;

    // Forward Enter and Return to MapEditor
    a = new QAction(icon, tr( "Edit heading","TaskEditor" ), this);
    a->setShortcut ( Qt::Key_Return);		
    a->setShortcutContext (Qt::WidgetWithChildrenShortcut);
    addAction (a);
    connect( a, SIGNAL( triggered() ), mainWindow, SLOT( editHeading() ) );
    a = new QAction( tr( "Edit heading","TaskEditor" ), this);
    a->setShortcut ( Qt::Key_Enter);			
    a->setShortcutContext (Qt::WidgetWithChildrenShortcut);
    addAction (a);
    connect( a, SIGNAL( triggered() ), mainWindow, SLOT( editHeading() ) );

    // Clone actions defined in MainWindow
    foreach (QAction* qa, mainWindow->taskEditorActions)
    {
        a = new QAction( this );
        a->setShortcut( qa->shortcut() );
        a->setShortcutContext (Qt::WidgetWithChildrenShortcut);
        connect( a, SIGNAL( triggered() ), qa, SLOT( trigger() ) );
        addAction(a);
    }

    mainLayout->addWidget (view);
    setLayout (mainLayout);

    view->setSelectionBehavior(QAbstractItemView::SelectRows);
    view->horizontalHeader()->setStretchLastSection(true);
    view->verticalHeader()->hide();
    view->setEditTriggers(QAbstractItemView::NoEditTriggers);

    filterActiveModel = new TaskFilterModel;
    filterActiveModel->setSourceModel(taskModel);

    view->setModel (filterActiveModel);
    view->setSortingEnabled(true);
    view->horizontalHeader()->setSortIndicator (0,Qt::AscendingOrder);

    blockExternalSelect=false;

    connect (
	view->selectionModel(),SIGNAL (selectionChanged (QItemSelection,QItemSelection)),
	this, SLOT (selectionChanged (QItemSelection,QItemSelection)));
    
    // layout changes trigger resorting
    connect( taskModel, SIGNAL( layoutChanged() ), this, SLOT(sort() ) );

    // Enable wordwrap when data changes
    connect ( 
        taskModel, SIGNAL( dataChanged( QModelIndex, QModelIndex)),
        view, SLOT( resizeRowsToContents() ) );
    connect ( 
        view->horizontalHeader(), SIGNAL( sectionResized(int, int, int)),
        view, SLOT( resizeRowsToContents() ) );


    // Initialize view filters according to previous settings
    setFilterMap();
    setFilterActive();
    setFilterNew();
    setFilterFlags();

    // Initialize column widths
    QString s;
    for (int i=0; i<6; i++)
    {
	s=QString("/taskeditor/columnWidth/%1").arg(i);
	if (settings.contains (s) )
	    view->setColumnWidth (i, settings.value(s, 100).toInt() );
    }

    // Initialize display of parents of a task
    bool ok;
    int i=settings.value ("/taskeditor/showParentsLevel", 0).toInt(&ok);
    if (ok) taskModel->setShowParentsLevel(i);

}

TaskEditor::~TaskEditor()
{
    settings.setValue ("/taskeditor/filterMap",actionToggleFilterMap->isChecked());
    settings.setValue ("/taskeditor/filterActive",actionToggleFilterActive->isChecked());
    settings.setValue ("/taskeditor/filterNew",actionToggleFilterNew->isChecked());
    settings.setValue ("/taskeditor/showParentsLevel",taskModel->getShowParentsLevel() );
    for (int i=0; i<7; i++)
	settings.setValue (QString("/taskeditor/columnWidth/%1").arg(i),view->columnWidth(i) );
}

void TaskEditor::setMapName (const QString &n)
{
    currentMapName=n;
    setFilterMap();
}

bool TaskEditor::isUsedFilterMap()
{
    return actionToggleFilterMap->isChecked();
}

void TaskEditor::setFilterMap () 
{
    if (isUsedFilterMap() )
        filterActiveModel->setMapFilter(currentMapName);
    else
        filterActiveModel->setMapFilter(QString() );
    sort();
}

bool TaskEditor::isUsedFilterActive()
{
    return actionToggleFilterActive->isChecked();
}

void TaskEditor::setFilterActive () 
{
    filterActiveModel->setFilter (actionToggleFilterActive->isChecked() );   
    sort();	
}

void TaskEditor::setFilterNew ()
{
    filterActiveModel->setFilterNew (actionToggleFilterNew->isChecked() );
    sort();
}

void TaskEditor::setFilterFlags ()  // FIXME-1 experimental
{
    filterActiveModel->setFilterFlags (actionToggleFilterFlags->isChecked() );
    sort();
}

void TaskEditor::showSelection()
{
    QModelIndexList list=view->selectionModel()->selectedIndexes();
    if (list.count()>0)
	// Usually whole row is selected, so just go for first cell
	view->scrollTo(taskModel->index(taskModel->getTask(list.first())), QAbstractItemView::EnsureVisible);
}

bool TaskEditor::select (Task *task)	
{
    if (task)
    {
	blockExternalSelect=true;
	QModelIndex i0b=taskModel->index (task); 
	QModelIndex i0e=taskModel->indexRowEnd (task); 

	QModelIndex i1b=filterActiveModel->mapFromSource(i0b ); 
	QModelIndex i1e=filterActiveModel->mapFromSource(i0e ); 

	QItemSelection sel (i1b, i1e);

	view->selectionModel()->select (sel, QItemSelectionModel::ClearAndSelect  );
	blockExternalSelect=false;
	return true;
    }
    return false;
}

void TaskEditor::clearSelection()
{
    view->selectionModel()->clearSelection();
}

void TaskEditor::selectionChanged ( const QItemSelection & selected, const QItemSelection & )
{// FIXME-3 what, if multiple selection in MapEditor?
    // Avoid segfault on quit, when selected is empty
    if (selected.indexes().isEmpty() ) return;

    QItemSelection sel0=filterActiveModel->mapSelectionToSource (selected);
    QModelIndex ix=sel0.indexes().first();
    Task *t=taskModel->getTask (ix);
    if (t) 
    {
	BranchItem *bi=t->getBranch();
	if (bi) 
	{
	    VymModel *m=bi->getModel();
	    if (!blockExternalSelect) m->select (bi);
	    if (m!=mainWindow->currentModel() )
		mainWindow->gotoModel (m);
	    view->setStyleSheet( 
	    QString ("selection-color: %1;" 
		     "selection-background-color: %2;").arg(bi->getHeadingColor().name() ).arg(m->getSelectionColor().name() ) );
	    view->scrollTo (selected.indexes().first() );   
	}
    }
}

void TaskEditor::contextMenuEvent ( QContextMenuEvent * e )
{
    taskContextMenu->popup (e->globalPos() );
}

void TaskEditor::sort()
{
    QHeaderView *hv=view->horizontalHeader();
    view->sortByColumn( hv->sortIndicatorSection(), hv->sortIndicatorOrder() );
    filterActiveModel->invalidate();	

}

void TaskEditor::toggleFilterMap ()
{
    setFilterMap ();
}

void TaskEditor::toggleFilterActive ()
{
    setFilterActive();

}
void TaskEditor::toggleFilterNew ()
{
    setFilterNew();
}

void TaskEditor::toggleFilterFlags ()
{
    setFilterFlags();
}