docs: describe the pmdaroot process interfaces
[pcp.git] / src / pmchart / searchdialog.cpp
blob67b7f5fb9beec34fa8e0fd252fb90031389a0a84
1 /*
2 * Copyright (c) 2007, Aconex. All Rights Reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * for more details.
14 #include "searchdialog.h"
15 #include <QMessageBox>
16 #include "main.h"
18 SearchDialog::SearchDialog(QWidget* parent) : QDialog(parent)
20 setupUi(this);
21 my.count = 0;
24 void SearchDialog::languageChange()
26 retranslateUi(this);
29 void SearchDialog::reset(QTreeWidget *pmns)
31 my.pmns = pmns;
32 buttonOk->setEnabled(false);
33 if (matchList->count() > 0)
34 buttonAll->setEnabled(true);
35 else
36 buttonAll->setEnabled(false);
37 changed();
38 listchanged();
41 void SearchDialog::clear()
43 this->hostPattern->clear();
44 this->metricPattern->clear();
45 this->instancePattern->clear();
46 this->resultStatus->setText("");
47 matchList->clear();
48 my.pmnsList.clear();
49 buttonSearch->setEnabled(false);
50 buttonOk->setEnabled(false);
51 buttonAll->setEnabled(false);
54 void SearchDialog::changed()
56 bool hostEnabled = (hostPattern->text() != QString::null);
57 bool metricEnabled = (metricPattern->text() != QString::null);
58 bool instanceEnabled = (instancePattern->text() != QString::null);
59 buttonSearch->setEnabled(hostEnabled || metricEnabled || instanceEnabled);
62 void SearchDialog::selectall()
64 matchList->selectAll();
67 void SearchDialog::listchanged()
69 QList<QListWidgetItem *> check = matchList->selectedItems();
70 if (check.size() == 0) {
71 buttonOk->setEnabled(false);
73 else {
74 buttonOk->setEnabled(true);
78 void SearchDialog::search()
80 QString res;
81 QTreeWidgetItemIterator iterator(my.pmns, QTreeWidgetItemIterator::All);
82 int h_match = 0;
83 int m_match = 0;
84 int i_match;
85 int count;
86 QRegExp h_rx;
87 QRegExp m_rx;
88 QRegExp i_rx;
90 console->post(PmChart::DebugUi,
91 "SearchDialog::search host=\"%s\" metric=\"%s\" instance=\"%s\"",
92 (const char *)hostPattern->text().toLatin1(),
93 (const char *)metricPattern->text().toLatin1(),
94 (const char *)instancePattern->text().toLatin1());
96 if (hostPattern->text() == QString::null &&
97 metricPattern->text() == QString::null &&
98 instancePattern->text() == QString::null) {
99 // got here via pressing Enter from one of the pattern input fields,
100 // and all the fields are empty ... do nothing
101 return;
104 if (hostPattern->text() != QString::null)
105 h_rx.setPattern(hostPattern->text());
107 if (metricPattern->text() != QString::null)
108 m_rx.setPattern(metricPattern->text());
110 if (instancePattern->text() != QString::null)
111 i_rx.setPattern(instancePattern->text());
113 matchList->clear();
114 my.pmnsList.clear();
115 count = 0;
116 for (; (*iterator); ++iterator) {
117 NameSpace *item = (NameSpace *)(*iterator);
118 if (item->isRoot()) {
119 // host name
120 if (hostPattern->text() != QString::null)
121 h_match = h_rx.indexIn(item->sourceName());
122 else
123 h_match = 0;
124 if (h_match >= 0) {
125 console->post(PmChart::DebugUi, "SearchDialog::search "
126 "host=\"%s\" h_match=%d",
127 (const char *)item->sourceName().toLatin1(), h_match);
129 item->setExpanded(true, false);
130 m_match = -2;
132 else if (h_match >= 0 && item->isMetric()) {
133 // metric name
134 count++;
135 if (metricPattern->text() != QString::null)
136 m_match = m_rx.indexIn(item->metricName());
137 else
138 m_match = 0;
139 if (m_match >= 0) {
140 if (item->isLeaf() &&
141 instancePattern->text() == QString::null) {
142 QString fqn = item->sourceName().append(":");
143 fqn.append(item->metricName());
144 matchList->addItem(fqn);
145 my.pmnsList.append(item);
146 m_match = -2;
148 console->post(PmChart::DebugUi, "SearchDialog::search "
149 "host=%s h_match=%d metric=%s m_match=%d",
150 (const char *)item->sourceName().toLatin1(), h_match,
151 (const char *)item->metricName().toLatin1(), m_match);
153 if (item->isLeaf() == false) {
154 // has instance domain
155 item->setExpanded(true, false);
156 count--;
160 else if (h_match >= 0 && m_match >= 0 && item->isInst()) {
161 // matched last metric, now related instance name ...
162 count++;
163 if (instancePattern->text() != QString::null)
164 i_match = i_rx.indexIn(item->metricInstance());
165 else
166 i_match = 0;
167 if (i_match >= 0) {
168 QString fqn = item->sourceName().append(":");
169 fqn.append(item->metricName());
170 fqn.append("[").append(item->metricInstance()).append("]");
171 matchList->addItem(fqn);
172 my.pmnsList.append(item);
174 console->post(PmChart::DebugUi, "SearchDialog::search "
175 "host=%s h_match=%d metric=%s m_match=%d inst=%s i_match=%d",
176 (const char *)item->sourceName().toLatin1(), h_match,
177 (const char *)item->metricName().toLatin1(), m_match,
178 (const char *)item->metricInstance().toLatin1(), i_match);
181 else if (item->isNonLeaf()) {
182 item->setExpanded(true, false);
183 m_match = -2;
185 else
186 m_match = -2;
189 if (matchList->count() > 0) {
190 buttonAll->setEnabled(true);
193 QTextStream(&res) << "Matched " << matchList->count() << " of " << count << " possibilities";
194 this->resultStatus->setText(res);
197 void SearchDialog::ok()
199 QList<QListWidgetItem *> selected = matchList->selectedItems();
200 int i;
201 int row;
202 NameSpace *parent;
204 for (i = 0; i < selected.size(); i++) {
205 row = matchList->row(selected[i]);
206 #if DESPERATE
207 fprintf(stderr, "[%d] %s:%s[%s]\n",
208 row, (const char *)my.pmnsList[row]->sourceName().toLatin1(),
209 (const char *)my.pmnsList[row]->metricName().toLatin1(),
210 (const char *)my.pmnsList[row]->metricInstance().toLatin1());
211 #endif
212 my.pmnsList[row]->setSelected(true);
213 parent = (NameSpace *)my.pmnsList[row]->parent();
214 while (parent->isRoot() == false) {
215 #if DESPERATE
216 fprintf(stderr, "SearchDialog::ok expand: %s\n",
217 (const char *)parent->metricName().toLatin1());
218 #endif
219 parent->QTreeWidgetItem::setExpanded(true);
220 parent = (NameSpace *)parent->parent();
224 accept();