@@ -57,39 +57,82 @@ TableView::TableView() {
5757 this ->verticalHeader ()->sectionResizeMode (QHeaderView::Fixed);
5858 this ->verticalHeader ()->hide ();
5959
60- auto action_cut = new QAction (" &Cut" , this );
61- QObject::connect (action_cut, &QAction::triggered, this , &TableView::cutSelection);
62- action_cut->setShortcuts (QKeySequence::Cut);
63- this ->addAction (action_cut);
64-
65- auto action_copy = new QAction (" Cop&y" , this );
66- QObject::connect (action_copy, &QAction::triggered, this , &TableView::copySelection);
67- action_copy->setShortcuts (QKeySequence::Copy);
68- this ->addAction (action_copy);
69-
70- auto action_paste = new QAction (" &Paste" , this );
71- QObject::connect (action_paste, &QAction::triggered, this , &TableView::pasteToSelection);
72- action_paste->setShortcuts (QKeySequence::Paste);
73- this ->addAction (action_paste);
74-
75- auto action_delete = new QAction (" &Delete" , this );
76- QObject::connect (action_delete, &QAction::triggered, this , &TableView::deleteSelection);
77- action_delete->setShortcut (QKeySequence::Delete);
78- action_delete->setShortcutContext (Qt::WidgetShortcut);
79- this ->addAction (action_delete);
60+ auto actionInsertAbove = new QAction (" Insert above" , this );
61+ QObject::connect (actionInsertAbove, &QAction::triggered, this , &TableView::insertAbove);
62+ this ->addAction (actionInsertAbove);
63+
64+ auto actionInsertBelow = new QAction (" Insert below" , this );
65+ QObject::connect (actionInsertBelow, &QAction::triggered, this , &TableView::insertBelow);
66+ this ->addAction (actionInsertBelow);
67+
68+ auto actionRemoveRow = new QAction (" Remove rows" , this );
69+ QObject::connect (actionRemoveRow, &QAction::triggered, this , &TableView::removeRows);
70+ this ->addAction (actionRemoveRow);
71+
72+ auto actionCut = new QAction (" &Cut" , this );
73+ QObject::connect (actionCut, &QAction::triggered, this , &TableView::cutSelection);
74+ actionCut->setShortcuts (QKeySequence::Cut);
75+ this ->addAction (actionCut);
76+
77+ auto actionCopy = new QAction (" Cop&y" , this );
78+ QObject::connect (actionCopy, &QAction::triggered, this , &TableView::copySelection);
79+ actionCopy->setShortcuts (QKeySequence::Copy);
80+ this ->addAction (actionCopy);
81+
82+ auto actionPaste = new QAction (" &Paste" , this );
83+ QObject::connect (actionPaste, &QAction::triggered, this , &TableView::pasteToSelection);
84+ actionPaste->setShortcuts (QKeySequence::Paste);
85+ this ->addAction (actionPaste);
86+
87+ auto actionDelete = new QAction (" &Delete" , this );
88+ QObject::connect (actionDelete, &QAction::triggered, this , &TableView::deleteSelection);
89+ actionDelete->setShortcut (QKeySequence::Delete);
90+ actionDelete->setShortcutContext (Qt::WidgetShortcut);
91+ this ->addAction (actionDelete);
8092
8193 this ->setContextMenuPolicy (Qt::CustomContextMenu);
82- QObject::connect (this , &TableView::customContextMenuRequested, [=](const QPoint& pos){
94+ QObject::connect (this , &TableView::customContextMenuRequested, [=](const QPoint& pos) {
8395 QMenu menu (this );
84- menu.addAction (action_cut );
85- menu.addAction (action_copy );
86- menu.addAction (action_paste );
96+ menu.addAction (actionInsertAbove );
97+ menu.addAction (actionInsertBelow );
98+ menu.addAction (actionRemoveRow );
8799 menu.addSeparator ();
88- menu.addAction (action_delete);
100+ menu.addAction (actionCut);
101+ menu.addAction (actionCopy);
102+ menu.addAction (actionPaste);
103+ menu.addSeparator ();
104+ menu.addAction (actionDelete);
89105 menu.exec (this ->viewport ()->mapToGlobal (pos));
90106 });
91107}
92108
109+ void TableView::insertAbove () {
110+ QModelIndex index = selectedIndexes ().at (0 );
111+ model ()->insertRow (index.row ());
112+ }
113+
114+ void TableView::insertBelow () {
115+ QModelIndex index = selectedIndexes ().at (0 );
116+ model ()->insertRow (index.row () + 1 );
117+ }
118+
119+ void TableView::removeRows () {
120+ // Collect all selected rows
121+ QSet<int > rows;
122+ for (const QModelIndex& index: selectedIndexes ()) {
123+ rows.insert (index.row ());
124+ }
125+
126+ // Convert to list and sort descending
127+ QList<int > rowList = rows.values ();
128+ std::sort (rowList.begin (), rowList.end (), std::greater<int >());
129+
130+ // Remove rows from bottom to top
131+ for (int row: rowList) {
132+ model ()->removeRow (row);
133+ }
134+ }
135+
93136void TableView::cutSelection () {
94137 copySelection ();
95138 deleteSelection ();
@@ -114,9 +157,9 @@ void TableView::pasteToSelection() {
114157 QString text = QApplication::clipboard ()->text ();
115158 QStringList rowContents = text.split (" \n " , Qt::SkipEmptyParts);
116159
117- QModelIndex initIndex = selectedIndexes ().at (0 );
118- int i0 = initIndex .row ();
119- int j0 = initIndex .column ();
160+ QModelIndex index = selectedIndexes ().at (0 );
161+ int i0 = index .row ();
162+ int j0 = index .column ();
120163
121164 for (int i = 0 ; i < rowContents.size (); ++i) {
122165 QStringList columnContents = rowContents.at (i).split (" \t " );
0 commit comments