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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
|
/*********************************************************************************
NixNote - An open-source client for the Evernote service.
Copyright (C) 2013 Randy Baumgarte
This program 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 2
of the License, or (at your option) any later version.
This program 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 this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
***********************************************************************************/
#include "notebookmenubutton.h"
#include "sql/notebooktable.h"
#include "sql/notetable.h"
#include <QMenu>
#include <QAction>
#include "global.h"
extern Global global;
NotebookMenuButton::NotebookMenuButton(QWidget *parent) :
QPushButton(parent)
{
setMenu(&rootMenu);
setIcon(global.getIconResource(":notebookSmallIcon"));
currentNotebookName = "";
loadData();
rootMenu.setFont(global.getGuiFont(rootMenu.font()));
this->setFont(global.getGuiFont(font()));
currentAction = 0;
}
void NotebookMenuButton::setCurrentNotebook(int lid, Note note) {
if (actions.size() == 0)
loadData();
if (lid <=0)
return;
blockSignals(true);
currentNoteLid = lid;
NotebookTable notebookTable(global.db);
Notebook notebook;
notebookTable.get(notebook, note.notebookGuid);
if (currentAction < actions.size())
actions[currentAction]->setChecked(false);
if (notebook.name.isSet())
this->setText(notebook.name);
for (int i=0; i<actions.size(); i++) {
actions[i]->setChecked(false);
QString notebookname = "";
if (notebook.name.isSet())
notebookname = notebook.name;
if (actions[i]->text().toLower().trimmed() == notebookname.toLower().trimmed()) {
currentAction = i;
actions[currentAction]->setChecked(true);
}
}
notebookLid = notebookTable.getLid(note.notebookGuid);
if (notebook.name.isSet())
notebookName = notebook.name;
blockSignals(false);
}
void NotebookMenuButton::updateCurrentNotebook(int notebookLid, QString notebookName) {
if (currentAction < actions.size())
actions[currentAction]->setChecked(false);
this->setText(notebookName);
for (int i=0; i<actions.size(); i++) {
actions[i]->setChecked(false);
if (actions[i]->text().toLower().trimmed() == notebookName.toLower().trimmed()) {
currentAction = i;
actions[currentAction]->setChecked(true);
}
}
this->notebookLid = notebookLid;
this->notebookName = notebookName;
blockSignals(false);
}
// Read in all of the data and build the menu.
void NotebookMenuButton::loadData() {
rootMenu.clear();
NotebookTable notebookTable(global.db);
QList<qint32> lids;
notebookTable.getAllOrderByName(lids);
if (notebookTable.findByName(currentNotebookName) <= 0)
currentNotebookName = "";
for (qint32 i=0; i<lids.size(); i++) {
Notebook book;
if (notebookTable.get(book, lids[i])) {
QAction *action = new QAction(this);
actions.append(action);
action->setText(book.name);
action->setCheckable(true);
connect(action, SIGNAL(triggered()), this, SLOT(notebookSelected()));
QFont f = action->font();
f.setPixelSize(10);
action->setFont(f);
QMenu *currentMenu = findStack(book);
addNotebookMenuItem(currentMenu, action);
if (currentNotebookName == "" && book.defaultNotebook.isSet() &&
book.defaultNotebook) {
currentNotebookName = book.name;
setText(currentNotebookName);
currentAction = actions.size()-1;
}
QString bookname = "";
if (book.name.isSet())
bookname = book.name;
if (bookname == currentNotebookName) {
action->setChecked(true);
}
}
}
}
// Add a new stack to the menu. The action item is a hidden action
// item with the menu's text.
void NotebookMenuButton::addStackMenuItem(QMenu *newMenu) {
QList<QAction*> items = rootMenu.actions();
for (int i=0; i<items.size(); i++) {
if ( newMenu->title().toLower() == items[i]->text().toLower() ) {
rootMenu.insertMenu(items[i], newMenu);
return;
}
}
}
// Add a new notebook menu item to the specified menu. This will
// be the root menu for anything without a stack, otherwise it
// will be the stack itself.
void NotebookMenuButton::addNotebookMenuItem(QMenu *menu, QAction *action) {
QList<QAction*> items = menu->actions();
for (int i=0; i<items.size(); i++) {
if ( action->text().toLower() == items[i]->text().toLower() ) {
menu->insertAction(items[i], action);
return;
}
}
action->setFont(global.getGuiFont(font()));
menu->addAction(action);
}
// Search through the list of known stack menu items & find the menu for
// this notebook's stack. If one doesn't exist we add it.
QMenu* NotebookMenuButton::findStack(Notebook n) {
QString stack = "";
if (n.stack.isSet())
stack = n.stack;
stack = stack.trimmed();
if (stack == "")
return &rootMenu;
for (int i=0; i<stackMenus.size(); i++) {
if (stackMenus.at(i)->title().toLower() == stack.toLower())
return stackMenus.at(i);
}
// Create a new stack. We add a dummy action item to the
// menu so we know where to add the menu later. This
// keeps things in sorted order
QMenu *newMenu = new QMenu(this);
newMenu->setTitle(stack);
QFont f = newMenu->font();
f.setBold(false);
newMenu->setFont(global.getGuiFont(f));
stackMenus.append(newMenu);
QAction *placeHolder = new QAction(this);
placeHolder->setVisible(false);
placeHolder->setText(stack);
addNotebookMenuItem(&rootMenu, placeHolder);
addStackMenuItem(newMenu);
return newMenu;
}
void NotebookMenuButton::notebookSelected() {
blockSignals(true);
if (!actions[currentAction]->isChecked())
actions[currentAction]->setChecked(true);
else {
actions[currentAction]->setChecked(false);
for (int i=0; i<actions.size(); i++) {
if (actions[i]->isChecked()) {
currentAction = i;
i=actions.size();
}
}
}
this->setText(actions[currentAction]->text());
blockSignals(false);
NoteTable noteTable(global.db);
NotebookTable notebookTable(global.db);
QString name = text();
qint32 notebookLid = notebookTable.findByName(name);
if (notebookLid > 0) {
noteTable.updateNotebook(currentNoteLid, notebookLid, true);
this->notebookLid = notebookLid;
this->notebookName = name;
emit(notebookChanged());
}
}
void NotebookMenuButton::reloadData() {
for (int i=actions.size()-1; i>=0; i--) {
delete actions[i];
}
for (int i=stackMenus.size()-1; i>=0; i--) {
delete stackMenus[i];
}
stackMenus.clear();
actions.clear();
loadData();
// Restore the proper notebook selection
if (currentNoteLid > 0) {
Note n;
NoteTable noteTable(global.db);
NotebookTable notebookTable(global.db);
if (noteTable.get(n, currentNoteLid, false, false)) {
QString notebookGuid = n.notebookGuid;
QList<qint32> bookList;
notebookTable.getAll(bookList);
QString bookName;
for (int i=0; i<bookList.size(); i++) {
Notebook book;
notebookTable.get(book, bookList[i]);
if (notebookGuid == book.guid) {
bookName = book.name;
i=bookList.size();
}
}
setCurrentNotebook(currentNoteLid, n);
}
}
return;
}
void NotebookMenuButton::reloadIcons() {
setIcon(global.getIconResource(":notebookSmallIcon"));
}
|