mirror of
https://thingvellir.net/git/overte
synced 2025-03-27 23:52:03 +01:00
Moved Bookmarks related funct out of Menu
Took everything Bookmarks related in Menu and moved it over to Bookmarks.
This commit is contained in:
parent
8996bf8e0d
commit
f34add9c9c
4 changed files with 161 additions and 145 deletions
|
@ -9,10 +9,20 @@
|
||||||
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
|
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
|
||||||
//
|
//
|
||||||
|
|
||||||
|
#include <QAction>
|
||||||
|
#include <QDebug>
|
||||||
|
#include <QJsonObject>
|
||||||
#include <QFile>
|
#include <QFile>
|
||||||
|
#include <QInputDialog>
|
||||||
#include <QJsonDocument>
|
#include <QJsonDocument>
|
||||||
|
#include <QMessageBox>
|
||||||
#include <QStandardPaths>
|
#include <QStandardPaths>
|
||||||
|
|
||||||
|
#include <AddressManager.h>
|
||||||
|
|
||||||
|
#include "MainWindow.h"
|
||||||
|
#include "Menu.h"
|
||||||
|
|
||||||
#include "Bookmarks.h"
|
#include "Bookmarks.h"
|
||||||
|
|
||||||
Bookmarks::Bookmarks() {
|
Bookmarks::Bookmarks() {
|
||||||
|
@ -71,3 +81,125 @@ void Bookmarks::persistToFile() {
|
||||||
QByteArray data = json.toJson();
|
QByteArray data = json.toJson();
|
||||||
saveFile.write(data);
|
saveFile.write(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Bookmarks::setupMenus(Menu* menubar, QMenu* menu) {
|
||||||
|
// Add menus/actions
|
||||||
|
menubar->addActionToQMenuAndActionHash(menu, MenuOption::BookmarkLocation, 0,
|
||||||
|
this, SLOT(bookmarkLocation()));
|
||||||
|
_bookmarksMenu = menu->addMenu(MenuOption::Bookmarks);
|
||||||
|
_deleteBookmarksAction = menubar->addActionToQMenuAndActionHash(menu, MenuOption::DeleteBookmark, 0,
|
||||||
|
this, SLOT(deleteBookmark()));
|
||||||
|
|
||||||
|
// Enable/Disable menus as needed
|
||||||
|
enableMenuItems(_bookmarks.count() > 0);
|
||||||
|
|
||||||
|
// Load bookmarks
|
||||||
|
for (auto it = _bookmarks.begin(); it != _bookmarks.end(); ++it ) {
|
||||||
|
QString bookmarkName = it.key();
|
||||||
|
QString bookmarkAddress = it.value().toString();
|
||||||
|
addLocationToMenu(menubar, bookmarkName, bookmarkAddress);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Bookmarks::bookmarkLocation() {
|
||||||
|
QInputDialog bookmarkLocationDialog(qApp->getWindow());
|
||||||
|
bookmarkLocationDialog.setWindowTitle("Bookmark Location");
|
||||||
|
bookmarkLocationDialog.setLabelText("Name:");
|
||||||
|
bookmarkLocationDialog.setInputMode(QInputDialog::TextInput);
|
||||||
|
bookmarkLocationDialog.resize(400, 200);
|
||||||
|
|
||||||
|
if (bookmarkLocationDialog.exec() == QDialog::Rejected) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
QString bookmarkName = bookmarkLocationDialog.textValue().trimmed();
|
||||||
|
bookmarkName = bookmarkName.replace(QRegExp("(\r\n|[\r\n\t\v ])+"), " ");
|
||||||
|
if (bookmarkName.length() == 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto addressManager = DependencyManager::get<AddressManager>();
|
||||||
|
QString bookmarkAddress = addressManager->currentAddress().toString();
|
||||||
|
|
||||||
|
Menu* menubar = Menu::getInstance();
|
||||||
|
if (contains(bookmarkName)) {
|
||||||
|
QMessageBox duplicateBookmarkMessage;
|
||||||
|
duplicateBookmarkMessage.setIcon(QMessageBox::Warning);
|
||||||
|
duplicateBookmarkMessage.setText("The bookmark name you entered already exists in your list.");
|
||||||
|
duplicateBookmarkMessage.setInformativeText("Would you like to overwrite it?");
|
||||||
|
duplicateBookmarkMessage.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
|
||||||
|
duplicateBookmarkMessage.setDefaultButton(QMessageBox::Yes);
|
||||||
|
if (duplicateBookmarkMessage.exec() == QMessageBox::No) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
removeLocationFromMenu(menubar, bookmarkName);
|
||||||
|
}
|
||||||
|
|
||||||
|
addLocationToMenu(menubar, bookmarkName, bookmarkAddress);
|
||||||
|
insert(bookmarkName, bookmarkAddress); // Overwrites any item with the same bookmarkName.
|
||||||
|
|
||||||
|
enableMenuItems(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Bookmarks::teleportToBookmark() {
|
||||||
|
QAction* action = qobject_cast<QAction*>(sender());
|
||||||
|
QString address = action->data().toString();
|
||||||
|
DependencyManager::get<AddressManager>()->handleLookupString(address);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Bookmarks::deleteBookmark() {
|
||||||
|
|
||||||
|
QStringList bookmarkList;
|
||||||
|
QList<QAction*> menuItems = _bookmarksMenu->actions();
|
||||||
|
for (int i = 0; i < menuItems.count(); i += 1) {
|
||||||
|
bookmarkList.append(menuItems[i]->text());
|
||||||
|
}
|
||||||
|
|
||||||
|
QInputDialog deleteBookmarkDialog(qApp->getWindow());
|
||||||
|
deleteBookmarkDialog.setWindowTitle("Delete Bookmark");
|
||||||
|
deleteBookmarkDialog.setLabelText("Select the bookmark to delete");
|
||||||
|
deleteBookmarkDialog.resize(400, 400);
|
||||||
|
deleteBookmarkDialog.setOption(QInputDialog::UseListViewForComboBoxItems);
|
||||||
|
deleteBookmarkDialog.setComboBoxItems(bookmarkList);
|
||||||
|
deleteBookmarkDialog.setOkButtonText("Delete");
|
||||||
|
|
||||||
|
if (deleteBookmarkDialog.exec() == QDialog::Rejected) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
QString bookmarkName = deleteBookmarkDialog.textValue().trimmed();
|
||||||
|
if (bookmarkName.length() == 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
removeLocationFromMenu(Menu::getInstance(), bookmarkName);
|
||||||
|
remove(bookmarkName);
|
||||||
|
|
||||||
|
if (_bookmarksMenu->actions().count() == 0) {
|
||||||
|
enableMenuItems(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Bookmarks::enableMenuItems(bool enabled) {
|
||||||
|
if (_bookmarksMenu) {
|
||||||
|
_bookmarksMenu->setEnabled(enabled);
|
||||||
|
}
|
||||||
|
if (_deleteBookmarksAction) {
|
||||||
|
_deleteBookmarksAction->setEnabled(enabled);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Bookmarks::addLocationToMenu(Menu* menubar, QString& name, QString& address) {
|
||||||
|
QAction* teleportAction = new QAction(_bookmarksMenu);
|
||||||
|
teleportAction->setData(address);
|
||||||
|
connect(teleportAction, SIGNAL(triggered()), this, SLOT(teleportToBookmark()));
|
||||||
|
|
||||||
|
menubar->addActionToQMenuAndActionHash(_bookmarksMenu, teleportAction,
|
||||||
|
name, 0, QAction::NoRole);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Bookmarks::removeLocationFromMenu(Menu* menubar, QString& name) {
|
||||||
|
menubar->removeAction(_bookmarksMenu, name);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -12,10 +12,13 @@
|
||||||
#ifndef hifi_Bookmarks_h
|
#ifndef hifi_Bookmarks_h
|
||||||
#define hifi_Bookmarks_h
|
#define hifi_Bookmarks_h
|
||||||
|
|
||||||
#include <QJsonObject>
|
|
||||||
#include <QDebug>
|
|
||||||
#include <QMap>
|
#include <QMap>
|
||||||
#include <QObject>
|
#include <QObject>
|
||||||
|
#include <QPointer>
|
||||||
|
|
||||||
|
class QAction;
|
||||||
|
class QMenu;
|
||||||
|
class Menu;
|
||||||
|
|
||||||
class Bookmarks: public QObject {
|
class Bookmarks: public QObject {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
@ -23,19 +26,32 @@ class Bookmarks: public QObject {
|
||||||
public:
|
public:
|
||||||
Bookmarks();
|
Bookmarks();
|
||||||
|
|
||||||
|
void setupMenus(Menu* menubar, QMenu* menu);
|
||||||
|
|
||||||
|
private slots:
|
||||||
|
void bookmarkLocation();
|
||||||
|
void teleportToBookmark();
|
||||||
|
void deleteBookmark();
|
||||||
|
|
||||||
|
private:
|
||||||
|
QVariantMap _bookmarks; // { name: address, ... }
|
||||||
|
|
||||||
|
QPointer<QMenu> _bookmarksMenu;
|
||||||
|
QPointer<QAction> _deleteBookmarksAction;
|
||||||
|
|
||||||
|
const QString BOOKMARKS_FILENAME = "bookmarks.json";
|
||||||
|
QString _bookmarksFilename;
|
||||||
|
|
||||||
void insert(const QString& name, const QString& address); // Overwrites any existing entry with same name.
|
void insert(const QString& name, const QString& address); // Overwrites any existing entry with same name.
|
||||||
void remove(const QString& name);
|
void remove(const QString& name);
|
||||||
bool contains(const QString& name) const;
|
bool contains(const QString& name) const;
|
||||||
|
|
||||||
QVariantMap* getBookmarks() { return &_bookmarks; };
|
|
||||||
|
|
||||||
private:
|
|
||||||
QVariantMap _bookmarks; // { name: address, ... }
|
|
||||||
|
|
||||||
const QString BOOKMARKS_FILENAME = "bookmarks.json";
|
|
||||||
QString _bookmarksFilename;
|
|
||||||
void readFromFile();
|
void readFromFile();
|
||||||
void persistToFile();
|
void persistToFile();
|
||||||
|
|
||||||
|
void enableMenuItems(bool enabled);
|
||||||
|
void addLocationToMenu(Menu* menubar, QString& name, QString& address);
|
||||||
|
void removeLocationFromMenu(Menu* menubar, QString& name);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // hifi_Bookmarks_h
|
#endif // hifi_Bookmarks_h
|
|
@ -125,15 +125,8 @@ Menu::Menu() :
|
||||||
appInstance, SLOT(toggleRunningScriptsWidget()));
|
appInstance, SLOT(toggleRunningScriptsWidget()));
|
||||||
|
|
||||||
addDisabledActionAndSeparator(fileMenu, "Location");
|
addDisabledActionAndSeparator(fileMenu, "Location");
|
||||||
addActionToQMenuAndActionHash(fileMenu, MenuOption::BookmarkLocation, 0,
|
qApp->getBookmarks()->setupMenus(this, fileMenu);
|
||||||
this, SLOT(bookmarkLocation()));
|
|
||||||
_bookmarksMenu = fileMenu->addMenu(MenuOption::Bookmarks);
|
|
||||||
_bookmarksMenu->setEnabled(false);
|
|
||||||
_deleteBookmarksMenu = addActionToQMenuAndActionHash(fileMenu,
|
|
||||||
MenuOption::DeleteBookmark, 0,
|
|
||||||
this, SLOT(deleteBookmark()));
|
|
||||||
_deleteBookmarksMenu->setEnabled(false);
|
|
||||||
loadBookmarks();
|
|
||||||
addActionToQMenuAndActionHash(fileMenu,
|
addActionToQMenuAndActionHash(fileMenu,
|
||||||
MenuOption::AddressBar,
|
MenuOption::AddressBar,
|
||||||
Qt::Key_Enter,
|
Qt::Key_Enter,
|
||||||
|
@ -1264,125 +1257,6 @@ void Menu::changeVSync() {
|
||||||
Application::getInstance()->setVSyncEnabled(isOptionChecked(MenuOption::RenderTargetFramerateVSyncOn));
|
Application::getInstance()->setVSyncEnabled(isOptionChecked(MenuOption::RenderTargetFramerateVSyncOn));
|
||||||
}
|
}
|
||||||
|
|
||||||
void Menu::loadBookmarks() {
|
|
||||||
QVariantMap* bookmarks = Application::getInstance()->getBookmarks()->getBookmarks();
|
|
||||||
if (bookmarks->count() > 0) {
|
|
||||||
|
|
||||||
QMapIterator<QString, QVariant> i(*bookmarks);
|
|
||||||
while (i.hasNext()) {
|
|
||||||
i.next();
|
|
||||||
|
|
||||||
QString bookmarkName = i.key();
|
|
||||||
QString bookmarkAddress = i.value().toString();
|
|
||||||
|
|
||||||
QAction* teleportAction = new QAction(getMenu(MenuOption::Bookmarks));
|
|
||||||
teleportAction->setData(bookmarkAddress);
|
|
||||||
connect(teleportAction, SIGNAL(triggered()), this, SLOT(teleportToBookmark()));
|
|
||||||
|
|
||||||
addActionToQMenuAndActionHash(_bookmarksMenu, teleportAction, bookmarkName, 0, QAction::NoRole);
|
|
||||||
}
|
|
||||||
|
|
||||||
_bookmarksMenu->setEnabled(true);
|
|
||||||
_deleteBookmarksMenu->setEnabled(true);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void Menu::bookmarkLocation() {
|
|
||||||
|
|
||||||
QInputDialog bookmarkLocationDialog(Application::getInstance()->getWindow());
|
|
||||||
bookmarkLocationDialog.setWindowTitle("Bookmark Location");
|
|
||||||
bookmarkLocationDialog.setLabelText("Name:");
|
|
||||||
bookmarkLocationDialog.setInputMode(QInputDialog::TextInput);
|
|
||||||
bookmarkLocationDialog.resize(400, 200);
|
|
||||||
|
|
||||||
if (bookmarkLocationDialog.exec() == QDialog::Rejected) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
QString bookmarkName = bookmarkLocationDialog.textValue().trimmed();
|
|
||||||
bookmarkName = bookmarkName.replace(QRegExp("(\r\n|[\r\n\t\v ])+"), " ");
|
|
||||||
if (bookmarkName.length() == 0) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto addressManager = DependencyManager::get<AddressManager>();
|
|
||||||
QString bookmarkAddress = addressManager->currentAddress().toString();
|
|
||||||
|
|
||||||
Bookmarks* bookmarks = Application::getInstance()->getBookmarks();
|
|
||||||
if (bookmarks->contains(bookmarkName)) {
|
|
||||||
QMessageBox duplicateBookmarkMessage;
|
|
||||||
duplicateBookmarkMessage.setIcon(QMessageBox::Warning);
|
|
||||||
duplicateBookmarkMessage.setText("The bookmark name you entered already exists in your list.");
|
|
||||||
duplicateBookmarkMessage.setInformativeText("Would you like to overwrite it?");
|
|
||||||
duplicateBookmarkMessage.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
|
|
||||||
duplicateBookmarkMessage.setDefaultButton(QMessageBox::Yes);
|
|
||||||
if (duplicateBookmarkMessage.exec() == QMessageBox::No) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
removeAction(_bookmarksMenu, bookmarkName);
|
|
||||||
}
|
|
||||||
|
|
||||||
QAction* teleportAction = new QAction(getMenu(MenuOption::Bookmarks));
|
|
||||||
teleportAction->setData(bookmarkAddress);
|
|
||||||
connect(teleportAction, SIGNAL(triggered()), this, SLOT(teleportToBookmark()));
|
|
||||||
|
|
||||||
QList<QAction*> menuItems = _bookmarksMenu->actions();
|
|
||||||
int position = 0;
|
|
||||||
while (position < menuItems.count() && bookmarkName > menuItems[position]->text()) {
|
|
||||||
position += 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
addActionToQMenuAndActionHash(_bookmarksMenu, teleportAction, bookmarkName, 0,
|
|
||||||
QAction::NoRole, position);
|
|
||||||
|
|
||||||
bookmarks->insert(bookmarkName, bookmarkAddress); // Overwrites any item with the same bookmarkName.
|
|
||||||
|
|
||||||
_bookmarksMenu->setEnabled(true);
|
|
||||||
_deleteBookmarksMenu->setEnabled(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Menu::teleportToBookmark() {
|
|
||||||
QAction *action = qobject_cast<QAction *>(sender());
|
|
||||||
QString address = action->data().toString();
|
|
||||||
DependencyManager::get<AddressManager>()->handleLookupString(address);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Menu::deleteBookmark() {
|
|
||||||
|
|
||||||
QStringList bookmarkList;
|
|
||||||
QList<QAction*> menuItems = _bookmarksMenu->actions();
|
|
||||||
for (int i = 0; i < menuItems.count(); i += 1) {
|
|
||||||
bookmarkList.append(menuItems[i]->text());
|
|
||||||
}
|
|
||||||
|
|
||||||
QInputDialog deleteBookmarkDialog(Application::getInstance()->getWindow());
|
|
||||||
deleteBookmarkDialog.setWindowTitle("Delete Bookmark");
|
|
||||||
deleteBookmarkDialog.setLabelText("Select the bookmark to delete");
|
|
||||||
deleteBookmarkDialog.resize(400, 400);
|
|
||||||
deleteBookmarkDialog.setOption(QInputDialog::UseListViewForComboBoxItems);
|
|
||||||
deleteBookmarkDialog.setComboBoxItems(bookmarkList);
|
|
||||||
deleteBookmarkDialog.setOkButtonText("Delete");
|
|
||||||
|
|
||||||
if (deleteBookmarkDialog.exec() == QDialog::Rejected) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
QString bookmarkName = deleteBookmarkDialog.textValue().trimmed();
|
|
||||||
if (bookmarkName.length() == 0) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
removeAction(_bookmarksMenu, bookmarkName);
|
|
||||||
|
|
||||||
Bookmarks* bookmarks = Application::getInstance()->getBookmarks();
|
|
||||||
bookmarks->remove(bookmarkName);
|
|
||||||
|
|
||||||
if (_bookmarksMenu->actions().count() == 0) {
|
|
||||||
_bookmarksMenu->setEnabled(false);
|
|
||||||
_deleteBookmarksMenu->setEnabled(false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void Menu::displayNameLocationResponse(const QString& errorString) {
|
void Menu::displayNameLocationResponse(const QString& errorString) {
|
||||||
|
|
||||||
if (!errorString.isEmpty()) {
|
if (!errorString.isEmpty()) {
|
||||||
|
@ -1753,3 +1627,4 @@ void Menu::setScriptsLocation(const QString& scriptsLocation) {
|
||||||
bumpSettings();
|
bumpSettings();
|
||||||
emit scriptLocationChanged(scriptsLocation);
|
emit scriptLocationChanged(scriptsLocation);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -80,6 +80,7 @@ class Menu : public QMenuBar {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
static Menu* getInstance();
|
static Menu* getInstance();
|
||||||
|
QMenu* getMenu(const QString& menuName);
|
||||||
|
|
||||||
void triggerOption(const QString& menuOption);
|
void triggerOption(const QString& menuOption);
|
||||||
QAction* getActionForOption(const QString& menuOption);
|
QAction* getActionForOption(const QString& menuOption);
|
||||||
|
@ -146,7 +147,6 @@ private:
|
||||||
QAction* getMenuAction(const QString& menuName);
|
QAction* getMenuAction(const QString& menuName);
|
||||||
int findPositionOfMenuItem(QMenu* menu, const QString& searchMenuItem);
|
int findPositionOfMenuItem(QMenu* menu, const QString& searchMenuItem);
|
||||||
int positionBeforeSeparatorIfNeeded(QMenu* menu, int requestedPosition);
|
int positionBeforeSeparatorIfNeeded(QMenu* menu, int requestedPosition);
|
||||||
QMenu* getMenu(const QString& menuName);
|
|
||||||
|
|
||||||
|
|
||||||
QHash<QString, QAction*> _actionHash;
|
QHash<QString, QAction*> _actionHash;
|
||||||
|
@ -244,9 +244,6 @@ private slots:
|
||||||
void editAttachments();
|
void editAttachments();
|
||||||
void editAnimations();
|
void editAnimations();
|
||||||
void changePrivateKey();
|
void changePrivateKey();
|
||||||
void bookmarkLocation();
|
|
||||||
void teleportToBookmark();
|
|
||||||
void deleteBookmark();
|
|
||||||
void hmdToolsClosed();
|
void hmdToolsClosed();
|
||||||
void runTests();
|
void runTests();
|
||||||
void showMetavoxelEditor();
|
void showMetavoxelEditor();
|
||||||
|
@ -318,10 +315,6 @@ private:
|
||||||
|
|
||||||
bool _shouldRenderTableNeedsRebuilding = true;
|
bool _shouldRenderTableNeedsRebuilding = true;
|
||||||
QMap<float, float> _shouldRenderTable;
|
QMap<float, float> _shouldRenderTable;
|
||||||
|
|
||||||
void loadBookmarks();
|
|
||||||
QMenu* _bookmarksMenu;
|
|
||||||
QAction* _deleteBookmarksMenu;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
namespace MenuOption {
|
namespace MenuOption {
|
||||||
|
|
Loading…
Reference in a new issue