-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtablesectorswidget.cpp
More file actions
121 lines (103 loc) · 2.85 KB
/
tablesectorswidget.cpp
File metadata and controls
121 lines (103 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include "tablesectorswidget.h"
#include "editsectorwidget.h"
#include "sectorsmodel.h"
#include <QTableView>
#include <QHeaderView>
#include <QPushButton>
#include <QVBoxLayout>
#include <QMenu>
#include <QEvent>
#include <QContextMenuEvent>
TableSectorsWidget::TableSectorsWidget(QWidget *parent)
: QWidget(parent), pModel(0)
{
tableView = new QTableView;
tableView->horizontalHeader()->setStretchLastSection(true);
tableView->setContextMenuPolicy(Qt::CustomContextMenu);
tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
connect(tableView, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(onContextMenuRequested(QPoint)));
QVBoxLayout *mainLayout = new QVBoxLayout(this);
mainLayout->addWidget(tableView);
createButtons();
createContextMenu();
}
void TableSectorsWidget::createButtons()
{
QVBoxLayout *mainLayout = qobject_cast<QVBoxLayout*>(layout());
QPushButton *editButton = new QPushButton(tr("Edit"));
QPushButton *addButton = new QPushButton(tr("Add"));
QPushButton *removeButton = new QPushButton(tr("Remove"));
connect(editButton, SIGNAL(clicked()), this, SLOT(editRow()));
connect(addButton, SIGNAL(clicked()), this, SLOT(addRow()));
connect(removeButton, SIGNAL(clicked()), this, SLOT(removeRow()));
QHBoxLayout *buttonLayout = new QHBoxLayout;
buttonLayout->addWidget(editButton);
buttonLayout->addStretch();
buttonLayout->addWidget(addButton);
buttonLayout->addWidget(removeButton);
mainLayout->addLayout(buttonLayout);
}
void TableSectorsWidget::createContextMenu()
{
contextMenu = new QMenu;
contextMenu->addAction(tr("Edit sector"), this, SLOT(editRow()));
contextMenu->addAction(tr("Remove sector"), this, SLOT(removeRow()));
}
void TableSectorsWidget::setModel(QAbstractTableModel *model)
{
pModel = qobject_cast<SectorsModel*>(model);
if (pModel)
{
tableView->setModel(pModel);
}
}
void TableSectorsWidget::onContextMenuRequested(QPoint pos)
{
if (tableView->indexAt(pos).isValid())
{
contextMenu->exec(tableView->viewport()->mapToGlobal(pos));
}
}
void TableSectorsWidget::editRow()
{
if (!pModel)
{
return;
}
QModelIndex index = tableView->currentIndex();
Sector sector = pModel->sector(index);
if (sector.isEmpty())
{
return;
}
EditSectorWidget *editSectorWidget = new EditSectorWidget(this);
editSectorWidget->setSector(sector);
if (editSectorWidget->exec() == QDialog::Accepted)
{
pModel->appendSector(editSectorWidget->sector());
}
}
void TableSectorsWidget::addRow()
{
if (!pModel)
{
return;
}
EditSectorWidget *editSectorWidget = new EditSectorWidget(this);
editSectorWidget->setSector(Sector());
if (editSectorWidget->exec() == QDialog::Accepted)
{
pModel->appendSector(editSectorWidget->sector());
}
}
void TableSectorsWidget::removeRow()
{
if (pModel)
{
QModelIndex index = tableView->currentIndex();
if (index.isValid())
{
pModel->removeRow(index.row());
}
}
}