Add CSV export button to counter viewer

This commit is contained in:
baldurk
2017-08-24 14:51:39 +01:00
parent 9c4aa3a50a
commit 760c04844a
4 changed files with 119 additions and 4 deletions
@@ -149,6 +149,9 @@
</item>
<item>
<widget class="QToolButton" name="saveCSV">
<property name="toolTip">
<string>Export to CSV</string>
</property>
<property name="text">
<string/>
</property>
@@ -37,6 +37,7 @@ PerformanceCounterViewer::PerformanceCounterViewer(ICaptureContext &ctx, QWidget
&PerformanceCounterViewer::CaptureCounters);
ui->captureCounters->setEnabled(m_Ctx.LogLoaded());
ui->saveCSV->setEnabled(m_Ctx.LogLoaded());
}
PerformanceCounterViewer::~PerformanceCounterViewer()
@@ -181,11 +182,13 @@ void PerformanceCounterViewer::CaptureCounters()
void PerformanceCounterViewer::OnLogfileClosed()
{
ui->captureCounters->setEnabled(false);
ui->saveCSV->setEnabled(false);
}
void PerformanceCounterViewer::OnLogfileLoaded()
{
ui->captureCounters->setEnabled(true);
ui->saveCSV->setEnabled(true);
}
void PerformanceCounterViewer::on_counterResults_doubleClicked(const QModelIndex &index)
@@ -201,3 +204,61 @@ void PerformanceCounterViewer::on_counterResults_doubleClicked(const QModelIndex
m_Ctx.SetEventID({}, eid, eid);
}
}
void PerformanceCounterViewer::on_saveCSV_clicked()
{
QString filename = RDDialog::getSaveFileName(this, tr("Export counter results as CSV"), QString(),
tr("CSV Files (*.csv)"));
if(!filename.isEmpty())
{
QDir dirinfo = QFileInfo(filename).dir();
if(dirinfo.exists())
{
QFile f(filename, this);
if(f.open(QIODevice::WriteOnly | QIODevice::Truncate))
{
QTextStream ts(&f);
for(int col = 0; col < ui->counterResults->columnCount(); col++)
{
ts << ui->counterResults->horizontalHeaderItem(col)->text();
if(col == ui->counterResults->columnCount() - 1)
ts << lit("\n");
else
ts << lit(",");
}
for(int row = 0; row < ui->counterResults->rowCount(); row++)
{
for(int col = 0; col < ui->counterResults->columnCount(); col++)
{
QTableWidgetItem *item = ui->counterResults->item(row, col);
if(item)
ts << item->text();
else
ts << lit("-");
if(col == ui->counterResults->columnCount() - 1)
ts << lit("\n");
else
ts << lit(",");
}
}
return;
}
RDDialog::critical(
this, tr("Error exporting counter results"),
tr("Couldn't open path %1 for write.\n%2").arg(filename).arg(f.errorString()));
}
else
{
RDDialog::critical(this, tr("Invalid directory"),
tr("Cannot find target directory to save to"));
}
}
}
@@ -50,6 +50,7 @@ public:
private slots:
// automatic slots
void on_counterResults_doubleClicked(const QModelIndex &index);
void on_saveCSV_clicked();
private:
QString FormatCounterResult(const CounterResult &result, const CounterDescription &description);
+54 -4
View File
@@ -44,10 +44,58 @@
<number>6</number>
</property>
<item>
<widget class="QToolButton" name="captureCounters">
<property name="text">
<string>Capture counters</string>
<widget class="QFrame" name="frame">
<property name="frameShape">
<enum>QFrame::Panel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<property name="spacing">
<number>2</number>
</property>
<property name="leftMargin">
<number>6</number>
</property>
<property name="topMargin">
<number>2</number>
</property>
<property name="rightMargin">
<number>6</number>
</property>
<property name="bottomMargin">
<number>2</number>
</property>
<item>
<widget class="QToolButton" name="captureCounters">
<property name="text">
<string>Capture counters</string>
</property>
</widget>
</item>
<item>
<widget class="Line" name="line">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="saveCSV">
<property name="toolTip">
<string>Export to CSV</string>
</property>
<property name="icon">
<iconset resource="../Resources/resources.qrc">
<normaloff>:/save.png</normaloff>:/save.png</iconset>
</property>
<property name="autoRaise">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
@@ -90,6 +138,8 @@
<header>Widgets/Extended/RDTableWidget.h</header>
</customwidget>
</customwidgets>
<resources/>
<resources>
<include location="../Resources/resources.qrc"/>
</resources>
<connections/>
</ui>