Files
clients/DataAnalyzer/stable.cpp

119 lines
3.1 KiB
C++

#include "stable.h"
#include <QKeyEvent>
#include <QApplication>
#include <qclipboard>
#include <QMenu>
#include <QDebug>
#include <QHeaderView>
#include <QMessageBox>
STable::STable(QWidget *parent):
QTableWidget(parent)
{
setSortingEnabled(true);
verticalHeader()->setContextMenuPolicy(Qt::CustomContextMenu);
connect(verticalHeader(),SIGNAL(customContextMenuRequested(QPoint)),SLOT(HeaderContextMenuShow(QPoint)));
connect(this,SIGNAL(cellClicked(int,int)),SLOT(CellClick(int,int)));
setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
m_nArticle = E_ARTICLE_NONE;
}
void STable::SetHeaderList(QVector <QStringList>* _pvecHead,int _nColumn)
{
setColumnCount(_pvecHead->size());
int i = 0;
foreach(QStringList strList, *_pvecHead )
setHorizontalHeaderItem(i++,new QTableWidgetItem(strList.at(_nColumn)));
}
void STable::keyPressEvent(QKeyEvent* event)
{
// If Ctrl-C typed
if (event->key() == Qt::Key_C && (event->modifiers() & Qt::ControlModifier))
{
QModelIndexList cells = selectedIndexes();
//qSort(cells);
QString text;
int currentRow = 0;
foreach (const QModelIndex& cell, cells)
{
if (text.length() == 0)
{
}
else if (cell.row() != currentRow)
text += '\n';
else
text += '\t';
currentRow = cell.row();
text += cell.data().toString();
}
QApplication::clipboard()->setText(text);
}
if (event->key() == Qt::Key_V && (event->modifiers() & Qt::ControlModifier))
{
Paste();
}
}
void STable::menuInsert()
{
int nRow = currentRow();
insertRow(nRow);
for (int i = 0; i < columnCount() ; i++)
setItem(nRow,i,new QTableWidgetItem(" "));
}
void STable::menuDelete()
{
int nCount = 0;
foreach(QModelIndex current,verticalHeader()->selectionModel()->selectedRows())
{
removeRow(current.row()-nCount);
nCount++;
}
}
void STable::HeaderContextMenuShow(const QPoint& pos) // this is a slot
{
QMenu myMenu;
myMenu.addAction("Insert", this, SLOT(menuInsert()));
myMenu.addAction("Delete", this, SLOT(menuDelete()));
myMenu.exec(mapToGlobal(pos));
}
void STable::CellClick(int nRow,int nPrev)
{
Q_UNUSED(nPrev);
if (rowHeight(nRow) == 30)
resizeRowToContents(nRow);
else
setRowHeight(nRow,30);
repaint();
}
void STable::Paste()
{
//if(qApp->clipboard()->mimeData()->hasHtml())
{
// TODO, parse the html data
}
//else
{
/*
QString selected_text = qApp->clipboard()->text();
QStringList cells = selected_text.split(QRegExp(QLatin1String("\\n|\\t")));
int cell = 0;
for(int row=0; row < rows; ++row)
{
for(int col=0; col < cols; ++col, ++cell)
{
QTableWidgetItem *newItem = new QTableWidgetItem(cells[cell]);
setItem(row, col, newItem);
}
}
*/
}
}