/* * Copyright 2015 Luca Beltrame * * This file is part of Danbooru Client. * * Danbooru Client 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. * * Danbooru Client 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 Danbooru Client. If not, see . */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "libdanbooru/danbooruservice.h" #include "libdanbooru/danboorupost.h" #include "libdanbooru/danboorupool.h" #include "model/danboorupostmodel.h" #include "model/danboorupoolmodel.h" #include "mainwindow.h" #include "danbooruconnectwidget.h" #include "danboorusettings.h" #include "generalpage.h" namespace Danbooru { QHash DanbooruMainWindow::ratingMap = { {0, DanbooruPost::Safe}, {1, DanbooruPost::Questionable}, {2, DanbooruPost::Explicit} }; DanbooruMainWindow::DanbooruMainWindow(QWidget *parent) : KXmlGuiWindow(parent), m_view(new QQuickWidget(this)), m_model(new DanbooruPostModel(this)), m_poolModel(new DanbooruPoolModel(this)), m_service(new DanbooruService()), m_tableView(new QTableView(this)), m_cache(0) { m_service->setParent(this); setCentralWidget(m_view); // QCoreApplication::setAttribute(Qt::AA_DontCreateNativeWidgetSiblings); qmlRegisterType("DanbooruClient", 1, 0, "DanbooruPost"); qmlRegisterType("DanbooruClient", 1, 0, "DanbooruService"); qRegisterMetaType(); loadSettings(); // TODO Configurable m_cache = new KImageCache(qApp->applicationName(), DanbooruSettings::self()->cacheSize()); m_service->setImageCache(m_cache); // Set up declarative bindings for the QQuickWidget m_view->setResizeMode(QQuickWidget::SizeRootObjectToView); KDeclarative::KDeclarative declarative; declarative.setDeclarativeEngine(m_view->engine()); declarative.setupBindings(); auto qmlViewPath = QStandardPaths::locate(QStandardPaths::GenericDataLocation, qApp->applicationName() + QChar('/') + QLatin1Literal("danbooruimageview.qml")); QQmlContext *ctxt = m_view->rootContext(); ctxt->setContextProperty("danbooruModel", m_model); ctxt->setContextProperty("danbooruService", m_service); m_view->setSource(QUrl::fromLocalFile(qmlViewPath)); QVector boardsList = { QUrl("http://konachan.com"), QUrl("https://yande.re") }; m_connectWidget = new DanbooruConnectWidget(boardsList); statusBar()->addPermanentWidget(m_connectWidget); m_connectWidget->hide(); // Set up PoolWidget m_tableView->setModel(m_poolModel); QDockWidget* dockWidget = new QDockWidget(i18n("Pools"), this); dockWidget->setAllowedAreas(Qt::BottomDockWidgetArea); dockWidget->setWidget(m_tableView); dockWidget->setObjectName("PoolView"); // Prevent the use of winId() when detached, leads to QQuickWidget bugs dockWidget->setFeatures(QDockWidget::NoDockWidgetFeatures); addDockWidget(Qt::BottomDockWidgetArea, dockWidget); dockWidget->show(); m_tableView->show(); // then, setup our actions setupActions(); // add a status bar statusBar()->show(); setupGUI(KXmlGuiWindow::ToolBar|Keys|Save|Create|StatusBar, "danbooru-clientui.rc"); // connections connect(m_connectWidget, &DanbooruConnectWidget::accepted, [this]() { m_service->setBoardUrl(m_connectWidget->boardUrl()); if (!m_connectWidget->isAnonymous() && !m_connectWidget->username().isEmpty() && !m_connectWidget->password().isEmpty()) { m_service->setUserName(m_connectWidget->username()); m_service->setPassword(m_connectWidget->password()); } actionCollection()->action(QLatin1String("fetch"))->setEnabled(true); m_connectWidget->hide(); m_service->getPostList(); }); connect(m_connectWidget, &DanbooruConnectWidget::rejected, [this]() { m_connectWidget->hide(); }); connect(m_service, &Danbooru::DanbooruService::postDownloaded, m_model, &Danbooru::DanbooruPostModel::addPost); connect(m_service, &Danbooru::DanbooruService::poolDownloaded, m_poolModel, &DanbooruPoolModel::addPool); connect(m_service, &Danbooru::DanbooruService::poolDownloadFinished, [this]() { m_tableView->resizeColumnsToContents(); } ); } DanbooruMainWindow::~DanbooruMainWindow() { } void DanbooruMainWindow::loadSettings() { m_service->setBlacklist(DanbooruSettings::self()->tagBlacklist()); m_service->setMaxPosts(DanbooruSettings::self()->maxPosts()); m_service->setMaximumAllowedRating(ratingMap.value(DanbooruSettings::self()->maxRating())); m_service->setMaxPosts(DanbooruSettings::self()->maxPosts()); } void DanbooruMainWindow::setupActions() { QAction *connectAction = new QAction( QIcon::fromTheme(QLatin1String("document-open-remote")), i18n("Connect..."), this); QAction *fetchAction = new QAction(QIcon::fromTheme(QLatin1String("download")), i18n("Download"), this); fetchAction->setEnabled(false); actionCollection()->addAction(QLatin1String("connect"), connectAction); actionCollection()->addAction(QLatin1String("fetch"), fetchAction); actionCollection()->setDefaultShortcut(connectAction, KStandardShortcut::Open); actionCollection()->setDefaultShortcut(fetchAction, KStandardShortcut::Find); // actionCollection()->removeAction(actionCollection()->action("help_contents")); KStandardAction::quit(qApp, SLOT(quit()), actionCollection()); KStandardAction::preferences(this, SLOT(optionsPreferences()), actionCollection()); connect(connectAction, &QAction::triggered, this, &DanbooruMainWindow::connectToBoard); connect(fetchAction, &QAction::triggered, this, &DanbooruMainWindow::downloadPosts); } void DanbooruMainWindow::connectToBoard() { if (!m_view) { return; } m_model->clear(); m_poolModel->clear(); m_service->reset(); m_connectWidget->show(); } void DanbooruMainWindow::downloadPosts() { if (!m_service) { return; } m_service->getPoolList(); } void DanbooruMainWindow::optionsPreferences() { KConfigDialog* dialog = new KConfigDialog(this, "danboorusettings", DanbooruSettings::self()); dialog->addPage(new GeneralPage(DanbooruSettings::self(), this), i18n("General"), "table"); connect(dialog, &KConfigDialog::settingsChanged, this, &DanbooruMainWindow::loadSettings); dialog->show(); } } // namespace Danbooru