restable: add menu entry to save table as csv file

This commit is contained in:
Jean-Francois Dockes
2011-08-18 19:02:39 +02:00
parent 4318891b48
commit 9c5886c7c4
4 changed files with 110 additions and 5 deletions
+61 -2
View File
@@ -32,6 +32,8 @@
#include <QPainter>
#include <QSplitter>
#include <QClipboard>
#include <QFileDialog>
#include <QMessageBox>
#include "recoll.h"
#include "refcntr.h"
@@ -143,8 +145,9 @@ void ResTableDetailArea::createPopupMenu(const QPoint& pos)
//// Data model methods
////
// Routines used to extract named data from an Rcl::Doc. The basic one just uses the meta map. Others
// (ie: the date ones) need to do a little processing
// Routines used to extract named data from an Rcl::Doc. The basic one
// just uses the meta map. Others (ie: the date ones) need to do a
// little processing
static string gengetter(const string& fld, const Rcl::Doc& doc)
{
map<string, string>::const_iterator it = doc.meta.find(fld);
@@ -362,6 +365,38 @@ QVariant RecollModel::data(const QModelIndex& index, int role) const
return QString::fromUtf8(lr.front().c_str());
}
void RecollModel::saveAsCSV(FILE *fp)
{
if (m_source.isNull())
return;
int cols = columnCount();
int rows = rowCount();
vector<string> tokens;
for (int col = 0; col < cols; col++) {
QString qs = headerData(col, Qt::Horizontal,Qt::DisplayRole).toString();
tokens.push_back((const char *)qs.toUtf8());
}
string csv;
stringsToCSV(tokens, csv);
fprintf(fp, "%s\n", csv.c_str());
tokens.clear();
for (int row = 0; row < rows; row++) {
Rcl::Doc doc;
if (!m_source->getDoc(row, doc)) {
continue;
}
for (int col = 0; col < cols; col++) {
tokens.push_back(m_getters[col](m_fields[col], doc));
}
stringsToCSV(tokens, csv);
fprintf(fp, "%s\n", csv.c_str());
tokens.clear();
}
}
// This gets called when the column headers are clicked
void RecollModel::sort(int column, Qt::SortOrder order)
{
@@ -594,6 +629,27 @@ void ResTable::resetSource()
setDocSource(RefCntr<DocSequence>());
}
void ResTable::saveAsCSV()
{
LOGDEB(("ResTable::saveAsCSV\n"));
if (!m_model)
return;
QString s =
QFileDialog::getSaveFileName(this, //parent
tr("Save table to CSV file"),
QString::fromLocal8Bit(path_home().c_str())
);
const char *tofile = s.toLocal8Bit();
FILE *fp = fopen(tofile, "w");
if (fp == 0) {
QMessageBox::warning(0, "Recoll",
tr("Can't open/create file: ") + s);
return;
}
m_model->saveAsCSV(fp);
fclose(fp);
}
// This is called when the sort order is changed from another widget
void ResTable::onSortDataChanged(DocSeqSortSpec spec)
{
@@ -806,6 +862,9 @@ void ResTable::createHeaderPopupMenu(const QPoint& pos)
popup->addAction(tr("&Reset sort"), this, SLOT(resetSort()));
popup->addSeparator();
popup->addAction(tr("&Save as CSV"), this, SLOT(saveAsCSV()));
popup->addSeparator();
popup->addAction(tr("&Delete column"), this, SLOT(deleteColumn()));
popup->addSeparator();
+2 -1
View File
@@ -42,8 +42,8 @@ public:
int role = Qt::DisplayRole ) const;
virtual QVariant data(const QModelIndex& index,
int role = Qt::DisplayRole ) const;
virtual void saveAsCSV(FILE *fp);
virtual void sort(int column, Qt::SortOrder order = Qt::AscendingOrder);
// Specific methods
virtual void readDocSource();
virtual void setDocSource(RefCntr<DocSequence> nsource);
@@ -136,6 +136,7 @@ public slots:
virtual void deleteColumn();
virtual void addColumn();
virtual void resetSort(); // Revert to natural (relevance) order
virtual void saveAsCSV();
virtual void linkWasClicked(const QUrl&);
virtual void makeRowVisible(int row);