File: sourcesettings.cpp

package info (click to toggle)
kcachegrind 4%3A4.14.0-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,800 kB
  • ctags: 3,227
  • sloc: cpp: 28,409; perl: 325; python: 235; sh: 5; makefile: 1
file content (212 lines) | stat: -rw-r--r-- 5,726 bytes parent folder | download | duplicates (3)
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
/* This file is part of KCachegrind.
   Copyright (C) 2009 Josef Weidendorfer <Josef.Weidendorfer@gmx.de>

   KCachegrind 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, version 2.

   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; see the file COPYING.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

/*
 * Source annotation directory settings config page
 */

#include <QTreeWidgetItem>
#include <QFileDialog>

#include "tracedata.h"
#include "sourcesettings.h"
#include "globalconfig.h"

//
// SourceSettings
//

SourceSettings::SourceSettings(TraceData* data, QWidget* parent)
    : ConfigPage(parent,
		 QObject::tr("Source Annotation"),
		 QObject::tr("Directory Settings for Source Annotation"))
{
    ui.setupUi(this);

    ui.dirList->setRootIsDecorated(false);

    GlobalConfig* c = GlobalConfig::config();
    _always = tr("(always)");

    QTreeWidgetItem* i;
    QStringList::const_iterator sit = c->generalSourceDirs().constBegin();
    for(; sit != c->generalSourceDirs().constEnd(); ++sit ) {
      QString d = (*sit);
      if (d.isEmpty()) d = "/";
      i = new QTreeWidgetItem();
      i->setText(0, _always);
      i->setText(1, d);
      ui.dirList->addTopLevelItem(i);
    }

    QStringList objItems(_always);
    if (data) {
	TraceObjectMap::Iterator oit;
      for ( oit = data->objectMap().begin();
	    oit != data->objectMap().end(); ++oit ) {
	QString n = (*oit).name();
	if (n.isEmpty()) continue;
	objItems << n;

	const QStringList& dirs = c->objectSourceDirs(n);
	sit = dirs.constBegin();
	for(; sit != dirs.constEnd(); ++sit ) {
	  QString d = (*sit);
	  if (d.isEmpty()) d = "/";
	  i = new QTreeWidgetItem();
	  i->setText(0, n);
	  i->setText(1, d);
	  ui.dirList->addTopLevelItem(i);
	}
      }
    }

    ui.objectBox->addItems(objItems);
    ui.objectBox->setCurrentIndex(0);

    connect(ui.addDirButton, SIGNAL(clicked()),
	    this, SLOT(addClicked()));
    connect(ui.deleteDirButton, SIGNAL(clicked()),
	    this, SLOT(deleteClicked()));
    connect(ui.browseDirButton, SIGNAL(clicked()),
	    this, SLOT(browseClicked()));
    connect(ui.dirList,
	    SIGNAL(currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)),
	    this,
	    SLOT(dirListItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)));
    connect(ui.objectBox,
	    SIGNAL(currentIndexChanged(QString)),
	    this, SLOT(objectChanged(QString)));
    connect(ui.dirEdit, SIGNAL(textChanged(QString)),
	    this, SLOT(dirEditChanged(QString)));

    _current = 0;
    update();
}

void SourceSettings::activate(QString s)
{
    int idx = s.toInt();
    if ((idx==0) || (idx>ui.dirList->topLevelItemCount())) return;

    ui.dirList->setCurrentItem(ui.dirList->topLevelItem(idx-1));
    ui.dirEdit->setFocus();
}

void SourceSettings::update()
{
    if (!_current) {
	ui.deleteDirButton->setEnabled(false);
	ui.objectBox->setEnabled(false);
	ui.dirEdit->setEnabled(false);
	ui.browseDirButton->setEnabled(false);
	return;
    }
    ui.deleteDirButton->setEnabled(true);
    ui.objectBox->setEnabled(true);
    ui.objectBox->setCurrentIndex(ui.objectBox->findText(_current->text(0)));
    ui.dirEdit->setEnabled(true);
    ui.dirEdit->setText(_current->text(1));
    ui.browseDirButton->setEnabled(true);
}

void SourceSettings::addClicked()
{
    QTreeWidgetItem* i = new QTreeWidgetItem();
    i->setText(0, ui.objectBox->currentText());
    i->setText(1, tr("<insert valid directory>"));
    ui.dirList->addTopLevelItem(i);
    ui.dirList->setCurrentItem(i);
}

void SourceSettings::deleteClicked()
{
    if (!_current) return;

    QTreeWidgetItem* i = _current;
    // the deletion removes the item
    delete _current;
    // deletion can trigger a call to dirListItemChanged() !
    if (_current == i) {
	_current = 0;
	update();
    }
}

void SourceSettings::browseClicked()
{
    QString d;
    d = QFileDialog::getExistingDirectory(this,
					  tr("Choose Source Directory"));
    if (!d.isEmpty())
	ui.dirEdit->setText(d);
}

void SourceSettings::dirListItemChanged(QTreeWidgetItem* current,
					QTreeWidgetItem*)
{
    _current = current;
    update();
}

void SourceSettings::objectChanged(QString obj)
{
    if (!_current) return;

    _current->setText(0, obj);
}

void SourceSettings::dirEditChanged(QString dir)
{
    if (!_current) return;

    _current->setText(1, dir);
}

bool SourceSettings::check(QString& errorMsg, QString& errorItem)
{
    for(int idx=0; idx< ui.dirList->topLevelItemCount(); idx++) {
	QTreeWidgetItem* item = ui.dirList->topLevelItem(idx);
	QString dir = item->text(1);
	if (QDir(dir).exists()) continue;
	errorMsg = tr("Directory does not exist");
	errorItem = QString("%1").arg(idx+1);
	return false;
    }
    return true;
}

void SourceSettings::accept()
{
    GlobalConfig* c = GlobalConfig::config();

    QHash<QString, QStringList> dirs;
    for(int idx=0; idx< ui.dirList->topLevelItemCount(); idx++) {
	QTreeWidgetItem* item = ui.dirList->topLevelItem(idx);
	dirs[item->text(0)] << item->text(1);
    }
    QHash<QString, QStringList>::const_iterator oit = dirs.constBegin();
    for(;oit != dirs.constEnd(); ++oit) {
	if (oit.key() == _always)
	    c->setGeneralSourceDirs(oit.value());
	else
	    c->setObjectSourceDirs(oit.key(), oit.value());
    }
}

#include "sourcesettings.moc"