Last active
December 14, 2015 23:59
-
-
Save loclamor/5169486 to your computer and use it in GitHub Desktop.
cut/copy/paste
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
project("SpreadSheet") | |
find_package(KDE4 REQUIRED) | |
include(KDE4Defaults) | |
include_directories(${KDE4_INCLUDES} ${CMAKE_BINARY_DIR}) | |
#ENABLE_TESTING(true) | |
set(spreadsheet_SRCS | |
main.cpp | |
SpreadSheetMainWindow.cpp | |
) | |
kde4_add_executable(spreadsheet ${spreadsheet_SRCS}) | |
target_link_libraries(spreadsheet ${KDE4_KDEUI_LIBS}) | |
set(testspreadsheet_SRCS | |
SpreadSheetMainWindow.cpp | |
TestSpreadSheet.cpp | |
) | |
kde4_add_unit_test(testSpreadSheet ${testspreadsheet_SRCS}) | |
target_link_libraries(testSpreadSheet ${KDE4_KDEUI_LIBS} ${QT_QTTEST_LIBRARY}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <QDebug> | |
#include <kcmdlineargs.h> | |
#include <kaboutdata.h> | |
#include <kapplication.h> | |
#include <QWidget> | |
#include "SpreadSheetMainWindow.h" | |
int main(){ | |
qDebug() <<"Starting"; | |
qDebug() << "QT_VERSION: " << QT_VERSION; | |
qDebug() << "QT_VERSION_STR: " << QT_VERSION_STR << endl; | |
KAboutData * kabout = new KAboutData( | |
QByteArray("Monapp"), | |
QByteArray(""), | |
ki18n("MonAppName"), | |
QByteArray("130.0")); | |
KCmdLineArgs::init(kabout); | |
KApplication app; | |
SpreadSheetMainWindow * mainWidget = new SpreadSheetMainWindow(); | |
mainWidget->setWindowTitle("Spread Sheet"); | |
mainWidget->show(); | |
return app.exec(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "SpreadSheetMainWindow.h" | |
#include <QDebug> | |
#include "kactioncollection.h" | |
#include <QTableWidget> | |
#include <KAction> | |
#include <QLabel> | |
#include <QMap> | |
#include "kstatusbar.h" | |
SpreadSheetMainWindow::SpreadSheetMainWindow(QWidget *parent) : KXmlGuiWindow(parent){ | |
table = new QTableWidget(100, 100); | |
table->setSelectionMode(QAbstractItemView::ContiguousSelection); | |
initTable(); | |
setCentralWidget(table); | |
setupActions(); | |
coords = new QLabel("[0;0]"); | |
formule = new QLabel("formule"); | |
statusBar()->addWidget(coords); | |
statusBar()->addWidget(formule); | |
//pour refresh coords et formule | |
connect(table->selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)), | |
this, SLOT(onTableSelection(QModelIndex))); | |
setupGUI(); | |
} | |
const QString SpreadSheetMainWindow::locationFromIndex(int row, int column){ | |
QString s; | |
column++; | |
do { | |
column--; | |
s = QString('A' + (column % 26)) + s; | |
column = column / 26 ; | |
} while(column > 0); | |
return s + QString::number(row + 1); | |
} | |
void SpreadSheetMainWindow::on_openNew(){ | |
qDebug() <<"openNew"; | |
} | |
void SpreadSheetMainWindow::on_open(){ | |
qDebug() <<"open"; | |
} | |
void SpreadSheetMainWindow::on_save(){ | |
qDebug() <<"save"; | |
} | |
void SpreadSheetMainWindow::on_saveAs(){ | |
qDebug() <<"saveAs"; | |
} | |
void SpreadSheetMainWindow::on_cut(){ | |
on_copy(); | |
QItemSelectionModel * selectionModel = table->selectionModel(); | |
foreach(const QModelIndex & curIndex, selectionModel->selection().at(0).indexes()){ | |
table->model()->setData(curIndex,QVariant()); | |
} | |
qDebug() << "data cuted"; | |
} | |
void SpreadSheetMainWindow::on_copy(){ | |
//copyData = table->model()->data(table->currentIndex()); | |
QItemSelectionModel * selectionModel = table->selectionModel(); | |
nbCopyRow = selectionModel->selection().at(0).height(); | |
copyData.clear(); | |
foreach(const QModelIndex & curIndex, selectionModel->selection().at(0).indexes()){ | |
copyData << table->model()->data(curIndex); | |
qDebug() << "data copyed : " << copyData.last().toString(); | |
} | |
} | |
void SpreadSheetMainWindow::on_paste(){ | |
int col, row, i = 0; | |
QModelIndex currentIndex = table->currentIndex(); | |
foreach(const QVariant & data, copyData) { | |
row = currentIndex.row() + (i % nbCopyRow) ; | |
col = currentIndex.column()+ (i / nbCopyRow); | |
i++; | |
QModelIndex index = table->model()->index(row,col); | |
table->model()->setData(index, data); | |
qDebug() << "data pasted : " << data.toString(); | |
} | |
} | |
void SpreadSheetMainWindow::onTableSelection(const QModelIndex & m_index){ | |
coords->setText("[ " + locationFromIndex(m_index.row(), m_index.column()) + " ]"); | |
formule->setText(table->model()->data(m_index).toString()); | |
} | |
void SpreadSheetMainWindow::setupActions(){ | |
//les actions | |
open = KStandardAction::open (this, SLOT(on_open()), actionCollection()); | |
openNew = KStandardAction::openNew (this, SLOT(on_openNew()), actionCollection()); | |
save = KStandardAction::save (this, SLOT(on_save()), actionCollection()); | |
saveAs = KStandardAction::saveAs (this, SLOT(on_saveAs()), actionCollection()); | |
cut = KStandardAction::cut (this, SLOT(on_cut()), actionCollection()); | |
copy = KStandardAction::copy (this, SLOT(on_copy()), actionCollection()); | |
paste = KStandardAction::paste (this, SLOT(on_paste()), actionCollection()); | |
table->setContextMenuPolicy(Qt::ActionsContextMenu); | |
table->addAction(copy); | |
table->addAction(cut); | |
table->addAction(paste); | |
} | |
void SpreadSheetMainWindow::initTable(){ | |
QAbstractItemModel * tableModel; | |
tableModel = table->model(); | |
tableModel->setData(tableModel->index(0,0),"William"); | |
tableModel->setData(tableModel->index(0,1),"Adama"); | |
tableModel->setData(tableModel->index(0,2),"24-02-1947"); | |
tableModel->setData(tableModel->index(1,0),"Saul"); | |
tableModel->setData(tableModel->index(1,1),"Tigh"); | |
tableModel->setData(tableModel->index(1,2),"22-03-1949"); | |
tableModel->setData(tableModel->index(2,0),"Lee"); | |
tableModel->setData(tableModel->index(2,1),"Adama"); | |
tableModel->setData(tableModel->index(2,2),"03-04-1973"); | |
tableModel->setData(tableModel->index(3,0),"Kara"); | |
tableModel->setData(tableModel->index(3,1),"Thrace"); | |
tableModel->setData(tableModel->index(3,2),"08-04-1980"); | |
tableModel->setData(tableModel->index(4,0),"Laura"); | |
tableModel->setData(tableModel->index(4,1),"Roslin"); | |
tableModel->setData(tableModel->index(4,2),"28-04-1952"); | |
tableModel->setData(tableModel->index(5,0),"Gaius"); | |
tableModel->setData(tableModel->index(5,1),"Baltar"); | |
tableModel->setData(tableModel->index(5,2),"04-06-1971"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <KXmlGuiWindow> | |
class QTableView; | |
class KAction; | |
class QLabel; | |
#include <QVariantList> | |
class QModelIndex; | |
class QString; | |
class SpreadSheetMainWindow : public KXmlGuiWindow { | |
Q_OBJECT | |
public: | |
explicit SpreadSheetMainWindow(QWidget *parent=0); | |
static const QString locationFromIndex(int row, int column); | |
KAction * open; | |
KAction * openNew; | |
KAction * save; | |
KAction * saveAs; | |
KAction * cut; | |
KAction * copy; | |
KAction * paste; | |
private slots: | |
void on_openNew(); | |
void on_open(); | |
void on_save(); | |
void on_saveAs(); | |
void on_cut(); | |
void on_copy(); | |
void on_paste(); | |
void onTableSelection(const QModelIndex & m_index); | |
private: | |
void setupActions(); | |
void initTable(); | |
QTableView * table; | |
QLabel * coords; | |
QLabel * formule; | |
QVariantList copyData; | |
int nbCopyRow; | |
}; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "SpreadSheetModel.h" | |
SpreadSheetModel::SpreadSheetModel(QObject *parent) : | |
QAbstractTableModel(parent) | |
{ | |
} | |
int SpreadSheetModel::rowCount(const QModelIndex &index) const | |
{ | |
if(!index.isValid()){ | |
return 1<<16; | |
} | |
else { | |
return 0; | |
} | |
} | |
int SpreadSheetModel::columnCount(const QModelIndex &index) const | |
{ | |
if(!index.isValid()){ | |
return 1<<16; | |
} | |
else { | |
return 0; | |
} | |
} | |
QVariant SpreadSheetModel::data(const QModelIndex &index, int role) const | |
{ | |
return QVariant(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <QAbstractTableModel> | |
class SpreadSheetModel : public QAbstractTableModel | |
{ | |
Q_OBJECT | |
public: | |
explicit SpreadSheetModel(QObject *parent = 0); | |
int rowCount(const QModelIndex &index = QModelIndex()) const; | |
int columnCount(const QModelIndex &index = QModelIndex()) const; | |
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; | |
signals: | |
public slots: | |
}; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <QObject> | |
#include <QString> | |
#include <QtTest> | |
#include "SpreadSheetMainWindow.h" | |
#include "SpreadSheetModel.h" | |
class TestSpreadSheet : public QObject { | |
Q_OBJECT | |
private slots: | |
void testSpreadSheetModel(){ | |
SpreadSheetModel ssm; | |
QCOMPARE(ssm.rowCount(), 65536); | |
QCOMPARE(ssm.columnCount(), 65536); | |
QModelIndex index = ssm.index(0,0); | |
QCOMPARE(ssm.data(index), QVariant()); | |
QCOMPARE(ssm.rowCount(index), 0); | |
QModelIndex index2 = ssm.index(65535,65535); | |
QCOMPARE(ssm.data(index2), QVariant()); | |
QCOMPARE(ssm.columnCount(index2), 0); | |
} | |
void testLocationFromIndex(){ | |
QString retour; | |
QFETCH(int, row); | |
QFETCH(int, column); | |
QFETCH(QString, expected); | |
retour = SpreadSheetMainWindow::locationFromIndex(row, column); | |
QCOMPARE(retour, expected); | |
} | |
void testLocationFromIndex_data(){ | |
QTest::addColumn<int>("row"); | |
QTest::addColumn<int>("column"); | |
QTest::addColumn<QString>("expected"); | |
QTest::newRow("A1") << 0 << 0 << "A1"; | |
QTest::newRow("B1") << 0 << 1 << "B1"; | |
QTest::newRow("C2") << 1 << 2 << "C2"; | |
QTest::newRow("C2") << 1 << 2 << "C2"; | |
QTest::newRow("Z1") << 0 << 25 << "Z1"; | |
QTest::newRow("AA1") << 0 << 26 << "AA1"; | |
QTest::newRow("AB1") << 0 << 27 << "AB1"; | |
QTest::newRow("AZ1") << 0 << 51 << "AZ1"; | |
QTest::newRow("BA1") << 0 << 52 << "BA1"; | |
QTest::newRow("BC1") << 0 << 54 << "BC1"; | |
QTest::newRow("ZZ1") << 0 << 701 << "ZZ1"; | |
QTest::newRow("ZZ1") << 0 << 702 << "AAA1"; | |
} | |
}; | |
QTEST_MAIN(TestSpreadSheet) | |
#include "TestSpreadSheet.moc" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment