summaryrefslogtreecommitdiff
path: root/src/pmchart/tab.cpp
blob: 3d5ef2c6e8a2ed011ba89e24e9321a9a2e44f0c8 (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
/*
 * Copyright (c) 2007-2008, Aconex.  All Rights Reserved.
 * Copyright (c) 2006, Ken McDonell.  All Rights Reserved.
 * 
 * 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.
 */
#include "main.h"
#include <QtGui/QMessageBox>
#include "openviewdialog.h"
#include "recorddialog.h"

Tab::Tab(): QWidget(NULL)
{
    my.currentGadget = -1;
    my.recording = false;
    my.splitter = NULL;
    my.group = NULL;
}

void Tab::init(QTabWidget *tab, GroupControl *group, QString label)
{
    my.splitter = new QSplitter(tab);
    my.splitter->setOrientation(Qt::Vertical);
    my.splitter->setSizePolicy(QSizePolicy::MinimumExpanding,
				QSizePolicy::MinimumExpanding);
    tab->blockSignals(true);
    tab->addTab(my.splitter, label);
    tab->blockSignals(false);
    my.group = group;
}

void Tab::addGadget(Gadget *gadget)
{
    if (gadgetCount())
	pmchart->updateHeight(PmChart::minimumChartHeight());
    my.gadgetsList.append(gadget);
    if (my.currentGadget == -1)
	setCurrentGadget(gadgetCount() - 1);
    gadget->showWidget();
    console->post("Tab::addChart: [%d]->Chart %p", my.currentGadget, gadget);
}

int Tab::deleteCurrent(void)
{
    return deleteGadget(my.currentGadget);
}

int Tab::deleteGadget(Gadget *gadget)
{
    for (int i = 0; i < gadgetCount(); i++)
	if (my.gadgetsList.at(i) == gadget)
	    return deleteGadget(i);
    return 0;
}

int Tab::deleteGadget(int index)
{
    Gadget *gadget = my.gadgetsList.at(index);
    int newCurrent = my.currentGadget;
    int oldCurrent = my.currentGadget;
    int oldCount = gadgetCount();
    int height = gadget->height();

    if (index < oldCurrent || index == oldCount - 1)
	newCurrent--;
    if (newCurrent < 0)
	my.currentGadget = -1;
    else
	setCurrent(my.gadgetsList.at(newCurrent));

    my.group->deleteGadget(gadget);
    my.gadgetsList.removeAt(index);
    delete gadget;

    if (gadgetCount() > 0)
	pmchart->updateHeight(-height);

    return my.currentGadget;
}

int Tab::gadgetCount(void)
{
    return my.gadgetsList.size();
}

int Tab::currentGadgetIndex(void)
{
    return my.currentGadget;
}

Gadget *Tab::gadget(int index)
{
    if (index >= gadgetCount())
	return NULL;
    return my.gadgetsList.at(index);
}

Gadget *Tab::currentGadget(void)
{
    if (my.currentGadget == -1)
	return NULL;
    return my.gadgetsList.at(my.currentGadget);
}

void Tab::setCurrent(Gadget *gadget)
{
    int index;

    for (index = 0; index < gadgetCount(); index++)
	if (my.gadgetsList.at(index) == gadget)
	    break;
    if (index == gadgetCount())
	abort();    // bad, bad, bad - attempted setCurrent on invalid gadget
    setCurrentGadget(index);
}

void Tab::setCurrentGadget(int index)
{
    if (index >= gadgetCount() || index == my.currentGadget)
        return;
    if (my.currentGadget >= 0)
        my.gadgetsList.at(my.currentGadget)->setCurrent(false);
    my.currentGadget = index;
    if (my.currentGadget >= 0)
        my.gadgetsList.at(my.currentGadget)->setCurrent(true);
}

void Tab::showGadgets()
{
    for (int index = 0; index < gadgetCount() - 1; index++)
	my.gadgetsList.at(index)->showWidget();
}

bool Tab::isArchiveSource(void)
{
    return my.group->isArchiveSource();
}

bool Tab::isRecording(void)
{
    return my.recording;
}

void Tab::addFolio(QString folio, QString view)
{
    my.view = view;
    my.folio = folio;
}

void Tab::addLogger(PmLogger *pmlogger, QString archive)
{
    my.loggerList.append(pmlogger);
    my.archiveList.append(archive);
}

bool Tab::startRecording(void)
{
    RecordDialog record(this);

    console->post("Tab::startRecording");
    record.init(this);
    if (record.exec() != QDialog::Accepted)
	my.recording = false;
    else {	// write pmlogger/pmchart/pmafm configs and start up loggers.
	console->post("Tab::startRecording starting loggers");
	record.startLoggers();
	my.recording = true;
    }
    return my.recording;
}

void Tab::stopRecording(void)
{
    QString msg = "Q\n", errmsg;
    int count = my.loggerList.size();
    int i, sts, error = 0;

    console->post("Tab::stopRecording stopping %d logger(s)", count);
    for (int i = 0; i < count; i++) {
	if (my.loggerList.at(i)->state() == QProcess::NotRunning) {
	    errmsg.append(tr("Record process (pmlogger) failed for host: "));
	    errmsg.append(my.loggerList.at(i)->host());
	    errmsg.append("\n");
	    error++;
	}
	else {
	    my.loggerList.at(i)->write(msg.toAscii());
	    my.loggerList.at(i)->terminate();
	}
    }

    for (i = 0; i < my.archiveList.size(); i++) {
	QString archive = my.archiveList.at(i);

	console->post("Tab::stopRecording opening archive %s",
			(const char *)archive.toAscii());
	if ((sts = archiveGroup->use(PM_CONTEXT_ARCHIVE, archive)) < 0) {
	    errmsg.append(tr("Cannot open PCP archive: "));
	    errmsg.append(archive);
	    errmsg.append("\n");
	    errmsg.append(tr(pmErrStr(sts)));
	    errmsg.append("\n");
	    error++;
	}
	else {
	    archiveGroup->updateBounds();
	    QmcSource source = archiveGroup->context()->source();
	    pmtime->addArchive(source.start(), source.end(),
				source.timezone(), source.host(), true);
	}
    }

    // If all is well, we can now create the new "Record" Tab.
    // Order of cleanup and changing Record mode state is different
    // in the error case to non-error case, this is important for
    // getting the window state correct (i.e. pmchart->enableUi()).

    if (error) {
	cleanupRecording();
	pmchart->setRecordState(false);
	QMessageBox::warning(this, pmProgname, errmsg,
		QMessageBox::Ok|QMessageBox::Default|QMessageBox::Escape,
		QMessageBox::NoButton, QMessageBox::NoButton);
    }
    else {
	// Make the current Tab stop recording before changing Tabs
	pmchart->setRecordState(false);

	Tab *tab = new Tab;
	console->post("Tab::stopRecording creating tab: delta=%.2f pos=%.2f",
			tosec(*pmtime->archiveInterval()),
			tosec(*pmtime->archivePosition()));
	// TODO: may need to update archive samples/visible?
	tab->init(pmchart->tabWidget(), archiveGroup, "Record");
	pmchart->addActiveTab(tab);
	OpenViewDialog::openView((const char *)my.view.toAscii());
	cleanupRecording();
    }
}

void Tab::cleanupRecording(void)
{
    my.recording = false;
    my.loggerList.clear();
    my.archiveList.clear();
    my.view = QString::null;
    my.folio = QString::null;
}

void Tab::queryRecording(void)
{
    QString msg = "?\n", errmsg;
    int i, error = 0, count = my.loggerList.size();

    console->post("Tab::stopRecording querying %d logger(s)", count);
    for (i = 0; i < count; i++) {
	if (my.loggerList.at(i)->state() == QProcess::NotRunning) {
	    errmsg.append(tr("Record process (pmlogger) failed for host: "));
	    errmsg.append(my.loggerList.at(i)->host());
	    errmsg.append("\n");
	    error++;
	}
	else {
	    my.loggerList.at(i)->write(msg.toAscii());
	}
    }

    if (error) {
	msg = "Q\n";	// if one fails, we shut down all loggers
	for (i = 0; i < count; i++)
	    my.loggerList.at(i)->write(msg.toAscii());
	cleanupRecording();
	pmchart->setRecordState(false);
	QMessageBox::warning(this, pmProgname, errmsg,
		QMessageBox::Ok|QMessageBox::Default|QMessageBox::Escape,
		QMessageBox::NoButton, QMessageBox::NoButton);
    }
}

void Tab::detachLoggers(void)
{
    QString msg = "D\n", errmsg;
    int error = 0, count = my.loggerList.size();

    console->post("Tab::detachLoggers detaching %d logger(s)", count);
    for (int i = 0; i < count; i++) {
	if (my.loggerList.at(i)->state() == QProcess::NotRunning) {
	    errmsg.append(tr("Record process (pmlogger) failed for host: "));
	    errmsg.append(my.loggerList.at(i)->host());
	    errmsg.append("\n");
	    error++;
	}
	else {
	    my.loggerList.at(i)->write(msg.toAscii());
	}
    }

    if (error) {
	cleanupRecording();
	pmchart->setRecordState(false);
	QMessageBox::warning(this, pmProgname, errmsg,
		QMessageBox::Ok|QMessageBox::Default|QMessageBox::Escape,
		QMessageBox::NoButton, QMessageBox::NoButton);
    }
    else {
	pmchart->setRecordState(false);
	cleanupRecording();
    }
}