-
Notifications
You must be signed in to change notification settings - Fork 2
/
favoritesdialog.cpp
95 lines (76 loc) · 2.51 KB
/
favoritesdialog.cpp
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
#include "favoritesdialog.h"
#include "ui_favoritesdialog.h"
FavoritesDialog::FavoritesDialog(QList<unv::FavoriteEntry> &favorites, QWidget *parent) :
QDialog(parent),
ui(new Ui::FavoritesDialog)
{
ui->setupUi(this);
// needs additional work
// ui->favoritesTable->addAction(ui->actionAdd);
ui->favoritesTable->addAction(ui->actionDelete);
for (unv::FavoriteEntry &fav : favorites) {
int row;
ui->favoritesTable->insertRow(row = ui->favoritesTable->rowCount());
QVariant favV = QVariant::fromValue<unv::FavoriteEntry *>(&fav);
for (int i = 0; i < 2; ++i) {
auto it = new QTableWidgetItem();
it->setData(Qt::UserRole, favV);
it->setText(i == 0 ? fav.name : fav.host);
ui->favoritesTable->setItem(row, i, it);
}
auto portItem = new QTableWidgetItem();
portItem->setData(Qt::EditRole, fav.port);
portItem->setData(Qt::UserRole, favV);
ui->favoritesTable->setItem(row, 2, portItem);
}
ui->favoritesTable->resizeColumnsToContents();
}
FavoritesDialog::~FavoritesDialog()
{
delete ui;
}
void FavoritesDialog::on_favoritesTable_itemChanged(QTableWidgetItem *item)
{
if (!item)
return;
if (!item->isSelected())
return;
unv::FavoriteEntry *fav = item->data(Qt::UserRole).value<unv::FavoriteEntry *>();
if (!fav)
return;
switch (item->column()) {
case 0:
fav->name = item->text();
break;
case 1:
fav->host = item->text();
break;
case 2:
fav->port = item->text().toUInt();
break;
default:
Q_ASSERT(false);
}
QPair<int, unv::FavoriteEntry> pair(item->row(), *fav);
for (int i = 0; i < m_modifiedFavorites.length(); ++i) {
const QPair<int, unv::FavoriteEntry> &x = m_modifiedFavorites.at(i);
if (x == pair)
m_modifiedFavorites.removeAt(i);
}
m_modifiedFavorites << pair;
}
void FavoritesDialog::on_actionAdd_triggered()
{
ui->favoritesTable->insertRow(ui->favoritesTable->rowCount());
ui->favoritesTable->editItem(ui->favoritesTable->item(ui->favoritesTable->rowCount() - 1, 0));
}
void FavoritesDialog::on_actionDelete_triggered()
{
QTableWidgetItem *current = ui->favoritesTable->currentItem();
if (!current)
return;
int row = current->row();
for (int i = 0; i < 2; ++i)
delete ui->favoritesTable->item(row, i);
ui->favoritesTable->removeRow(row);
}