-
Notifications
You must be signed in to change notification settings - Fork 5
/
menulist.cpp
179 lines (161 loc) · 5.14 KB
/
menulist.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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/****************************************************************************
** Copyright (C) 2017 Olaf Japp
**
** This file is part of FlatSiteBuilder.
**
** FlatSiteBuilder is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** FlatSiteBuilder is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with FlatSiteBuilder. If not, see <http://www.gnu.org/licenses/>.
**
****************************************************************************/
#include "menulist.h"
#include "mainwindow.h"
#include "menueditor.h"
#include "menu.h"
#include "commands.h"
#include "flatbutton.h"
#include <QStatusBar>
#include <QGridLayout>
#include <QLabel>
#include <QHeaderView>
#include <QUndoCommand>
#include <QUndoStack>
#include <QXmlStreamWriter>
#include <QTest>
#include "flatbutton.h"
#include "tablecellbuttons.h"
MenuList::MenuList(MainWindow *win, Site *site)
{
m_win = win;
m_site = site;
m_menuInEditor = NULL;
m_editor = NULL;
m_titleLabel->setText("Menus");
m_filename = site->sourcePath() + "/Menus.xml";
QPushButton *button = new QPushButton("Add Menu");
button->setMaximumWidth(120);
m_list = new QTableWidget(0, 2, this);
m_list->verticalHeader()->hide();
m_list->setSelectionMode(QAbstractItemView::SingleSelection);
m_list->setSelectionBehavior(QAbstractItemView::SelectRows);
m_list->horizontalHeader()->setSectionResizeMode(1, QHeaderView::Stretch );
m_list->setToolTip("Double click to edit item");
QStringList labels;
labels << "" << "Name";
m_list->setHorizontalHeaderLabels(labels);
m_layout->addWidget(button, 1, 0);
m_layout->addWidget(m_list, 2, 0, 1, 3);
load();
connect(button, SIGNAL(clicked(bool)), this, SLOT(buttonClicked()));
connect(m_list, SIGNAL(cellDoubleClicked(int,int)), this, SLOT(tableDoubleClicked(int, int)));
}
void MenuList::registerMenuEditor(MenuEditor *editor)
{
m_editor = editor;
m_editor->registerUndoStack(m_undoStack);
connect(m_editor, SIGNAL(menuChanged(QString)), this, SLOT(menuChanged(QString)));
}
void MenuList::unregisterMenuEditor()
{
m_editor = NULL;
}
void MenuList::load()
{
m_list->clearContents();
m_list->setRowCount(0);
m_site->loadMenus();
int menuId = -1;
if(m_menuInEditor)
menuId = m_menuInEditor->id();
m_menuInEditor = NULL;
foreach(Menu *menu, m_site->menus())
{
QTableWidgetItem *item = addListItem(menu);
if(menu->id() == menuId)
{
m_list->selectRow(m_list->rowCount() - 1);
m_menuInEditor = menu;
emit editedItemChanged(item);
}
}
if(m_editor)
m_editor->reloadMenu(m_menuInEditor);
}
void MenuList::save()
{
m_site->saveMenus();
}
void MenuList::menuChanged(QString text)
{
contentChanged(text);
}
QTableWidgetItem *MenuList::addListItem(Menu *menu)
{
int rows = m_list->rowCount();
m_list->setRowCount(rows + 1);
TableCellButtons *tcb = new TableCellButtons;
tcb->setItem(menu);
connect(tcb, SIGNAL(deleteItem(QObject*)), this, SLOT(deleteMenu(QObject*)));
connect(tcb, SIGNAL(editItem(QObject*)), this, SLOT(editMenu(QObject*)));
m_list->setCellWidget(rows, 0, tcb);
m_list->setRowHeight(rows, tcb->sizeHint().height());
QTableWidgetItem *titleItem = new QTableWidgetItem(menu->name());
titleItem->setFlags(titleItem->flags() ^ Qt::ItemIsEditable);
titleItem->setData(Qt::UserRole, QVariant::fromValue(menu));
m_list->setItem(rows, 1, titleItem);
return titleItem;
}
void MenuList::buttonClicked()
{
Menu *menu = new Menu();
m_site->addMenu(menu);
addListItem(menu);
m_list->selectRow(m_list->rowCount() - 1);
menuChanged("menu added");
tableDoubleClicked(m_list->rowCount() - 1, 0);
}
void MenuList::deleteMenu(QObject *menu)
{
for(int row = 0; row < m_list->rowCount(); row++)
{
QTableWidgetItem *item = m_list->item(row, 1);
Menu *m = qvariant_cast<Menu*>(item->data(Qt::UserRole));
if(m == menu)
{
m_site->removeMenu(m);
m_list->removeRow(row);
menuChanged("menu \"" + m->name() + "\" deleted");
break;
}
}
}
void MenuList::editMenu(QObject *menu)
{
for(int row = 0; row < m_list->rowCount(); row++)
{
QTableWidgetItem *item = m_list->item(row, 1);
Menu *m = qvariant_cast<Menu*>(item->data(Qt::UserRole));
if(m == menu)
{
m_menuInEditor = m;
m_list->selectRow(row);
emit editContent(item);
break;
}
}
}
void MenuList::tableDoubleClicked(int r, int)
{
QTableWidgetItem *item = m_list->item(r, 1);
m_menuInEditor = qvariant_cast<Menu*>(item->data(Qt::UserRole));
emit editContent(item);
}