]> git.cworth.org Git - apitrace/blob - gui/mainwindow.cpp
74d9f94cc7742e10df6c54ff5717f47abb798cce
[apitrace] / gui / mainwindow.cpp
1 #include "mainwindow.h"
2
3 #include "apitracecall.h"
4 #include "apicalldelegate.h"
5 #include "apitracemodel.h"
6 #include "apitracefilter.h"
7
8 #include <QAction>
9 #include <QDebug>
10 #include <QDir>
11 #include <QFileDialog>
12 #include <QLineEdit>
13 #include <QMessageBox>
14 #include <QProcess>
15 #include <QToolBar>
16 #include <QWebView>
17
18
19 MainWindow::MainWindow()
20     : QMainWindow(),
21       m_replayProcess(0)
22 {
23     m_ui.setupUi(this);
24
25     m_model = new ApiTraceModel();
26     m_proxyModel = new ApiTraceFilter();
27     m_proxyModel->setSourceModel(m_model);
28     m_ui.callView->setModel(m_proxyModel);
29     m_ui.callView->setItemDelegate(new ApiCallDelegate);
30     for (int column = 0; column < m_model->columnCount(); ++column)
31         m_ui.callView->resizeColumnToContents(column);
32
33     QToolBar *toolBar = addToolBar(tr("Navigation"));
34     m_filterEdit = new QLineEdit(toolBar);
35     toolBar->addWidget(m_filterEdit);
36
37     m_ui.detailsDock->hide();
38
39     connect(m_ui.actionOpen, SIGNAL(triggered()),
40             this, SLOT(openTrace()));
41     connect(m_ui.actionQuit, SIGNAL(triggered()),
42             this, SLOT(close()));
43
44     connect(m_ui.actionReplay, SIGNAL(triggered()),
45             this, SLOT(replayStart()));
46     connect(m_ui.actionStop, SIGNAL(triggered()),
47             this, SLOT(replayStop()));
48
49     connect(m_ui.callView, SIGNAL(activated(const QModelIndex &)),
50             this, SLOT(callItemSelected(const QModelIndex &)));
51     connect(m_filterEdit, SIGNAL(returnPressed()),
52             this, SLOT(filterTrace()));
53 }
54
55 void MainWindow::openTrace()
56 {
57     QString fileName =
58         QFileDialog::getOpenFileName(
59             this,
60             tr("Open Trace"),
61             QDir::homePath(),
62             tr("Trace Files (*.trace)"));
63
64     qDebug()<< "File name : " <<fileName;
65
66     newTraceFile(fileName);
67     m_model->loadTraceFile(fileName);
68 }
69
70 void MainWindow::loadTrace(const QString &fileName)
71 {
72     if (!QFile::exists(fileName)) {
73         QMessageBox::warning(this, tr("File Missing"),
74                              tr("File '%1' doesn't exist.").arg(fileName));
75         return;
76     }
77     qDebug()<< "Loading  : " <<fileName;
78
79     m_model->loadTraceFile(fileName);
80     newTraceFile(fileName);
81 }
82
83 void MainWindow::callItemSelected(const QModelIndex &index)
84 {
85     ApiTraceCall *call = index.data().value<ApiTraceCall*>();
86     if (call) {
87         m_ui.detailsWebView->setHtml(call->toHtml());
88         m_ui.detailsDock->show();
89     } else {
90         m_ui.detailsDock->hide();
91     }
92 }
93
94 void MainWindow::filterTrace()
95 {
96     m_proxyModel->setFilterString(m_filterEdit->text());
97 }
98
99 void MainWindow::replayStart()
100 {
101     if (!m_replayProcess) {
102 #ifdef Q_OS_WIN
103         QString format = QLatin1String("%1;");
104 #else
105         QString format = QLatin1String("%1:");
106 #endif
107         QString buildPath = format.arg(BUILD_DIR);
108         QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
109         env.insert("PATH", buildPath + env.value("PATH"));
110
111         qputenv("PATH", env.value("PATH").toLatin1());
112
113         m_replayProcess = new QProcess(this);
114         m_replayProcess->setProcessEnvironment(env);
115
116         connect(m_replayProcess, SIGNAL(finished(int, QProcess::ExitStatus)),
117                 this, SLOT(replayFinished()));
118         connect(m_replayProcess, SIGNAL(error(QProcess::ProcessError)),
119                 this, SLOT(replayError(QProcess::ProcessError)));
120     }
121
122     if (m_traceFileName.isEmpty())
123         return;
124
125     QStringList arguments;
126     arguments << m_traceFileName;
127
128     m_replayProcess->start(QLatin1String("glretrace"),
129                            arguments);
130
131     m_ui.actionStop->setEnabled(true);
132     m_ui.actionReplay->setEnabled(false);
133 }
134
135 void MainWindow::replayStop()
136 {
137     if (m_replayProcess) {
138         m_replayProcess->kill();
139
140         m_ui.actionStop->setEnabled(false);
141         m_ui.actionReplay->setEnabled(true);
142     }
143 }
144
145 void MainWindow::newTraceFile(const QString &fileName)
146 {
147     m_traceFileName = fileName;
148
149     if (m_traceFileName.isEmpty()) {
150         m_ui.actionReplay->setEnabled(false);
151     } else {
152         m_ui.actionReplay->setEnabled(true);
153     }
154 }
155
156 void MainWindow::replayFinished()
157 {
158     m_ui.actionStop->setEnabled(false);
159     m_ui.actionReplay->setEnabled(true);
160
161     QString output = m_replayProcess->readAllStandardOutput();
162
163 #if 0
164     qDebug()<<"Process finished = ";
165     qDebug()<<"\terr = "<<m_replayProcess->readAllStandardError();
166     qDebug()<<"\tout = "<<output;
167 #endif
168
169     if (output.length() < 80) {
170         statusBar()->showMessage(output);
171     }
172 }
173
174 void MainWindow::replayError(QProcess::ProcessError err)
175 {
176     m_ui.actionStop->setEnabled(false);
177     m_ui.actionReplay->setEnabled(true);
178
179     qDebug()<<"Process error = "<<err;
180     qDebug()<<"\terr = "<<m_replayProcess->readAllStandardError();
181     qDebug()<<"\tout = "<<m_replayProcess->readAllStandardOutput();
182     QMessageBox::warning(
183         this, tr("Replay Failed"),
184         tr("Couldn't execute the replay file '%1'").arg(m_traceFileName));
185 }
186
187 #include "mainwindow.moc"