/*** The MIT License Copyright (c) 2018 Teppei Tamra (TAM) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ***/ #include <QMdiSubWindow> #include <QPixmap> #include <QLabel> #include "saoridef.h" #include "saoriwindow.h" #include "ui_saoriwindow.h" #include "saoriapplication.h" #include "saoriaddaccountdialog.h" #include "saoriview.h" SaoriWindow::SaoriWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::SaoriWindow) { ui->setupUi(this); m_configDialog = new SaoriConfigureDialog(); m_configDialog->setModal(true); ui->mdiArea->setActivationOrder(QMdiArea::ActivationHistoryOrder); connect(m_configDialog,&SaoriConfigureDialog::accountsUpdated,this,&SaoriWindow::updateTimelineList); updateTimelineList(); } SaoriWindow::~SaoriWindow() { delete ui; delete m_configDialog; } QPair<QString,QString> SaoriWindow::getTimelineTitle(const QString timeline) { // trick for translation. auto t = timeline.split(':'); QStringList trtl; QString tlText,icon; trtl << "home" << "local" << "public" << "notifications" << "instance" << "accounts"; switch (trtl.indexOf(t[0])) { case 0: tlText = tr("home"); icon = ":/icons/ionicons/chatbubbles.svg"; break; case 1: tlText = tr("local"); icon = ":/icons/ionicons/chatbubble.svg"; break; case 2: tlText = tr("public"); icon = ":/icons/ionicons/earth.svg"; break; case 3: tlText = tr("notifications"); icon = ":/icons/ionicons/alert.svg"; break; case 4: tlText = tr("instance"); icon = ":/icons/ionicons/earth.svg"; break; case 5: tlText = tr("accounts"); icon = ":/icons/ionicons/person.svg"; break; default: tlText = timeline; break; } if (t.count() > 1) for(int i = 1;i < t.count();i ++) tlText += ":" + t[i]; return QPair<QString,QString>(tlText,icon); } void SaoriWindow::openView(const QStringList viewName) { if (auto view = SaoriView::findView(viewName.at(1),viewName.at(0))) { ui->mdiArea->setActiveSubWindow(qobject_cast<QMdiSubWindow*>(view->parent())); return; } QString title = getTimelineTitle(viewName.at(1)).first + ":" + viewName.at(0); auto view = new SaoriView(viewName.at(1),viewName.at(0)); auto sub = ui->mdiArea->addSubWindow(view); connect(view,&SaoriView::openMediaView,this,&SaoriWindow::openMediaView); connect(view,&SaoriView::openAccountView,this,&SaoriWindow::openView); sub->setWindowTitle(title); sub->setWindowIcon(QIcon(getTimelineTitle(viewName.at(1)).second)); auto setting = SaoriApplication::saori()->setting(); QString c = SAORI_SETTING_VIEW; c += "/" + viewName.at(0) + "/" + viewName.at(1); QRect defRect((ui->mdiArea->subWindowList().count() - 1) * 24, (ui->mdiArea->subWindowList().count() - 1) * 24, 450, 600); if (setting->value(c + SAORI_SETTING_VIEW_PINED,false).toBool()) { sub->setGeometry(setting->value(c + SAORI_SETTING_VIEW_GEOMETRY,defRect).toRect()); } else { sub->setGeometry(defRect); } sub->show(); } void SaoriWindow::openMediaView(const QUrl url) { // TODO 専用のwidget作った方がいいでしょう。 auto view = new QLabel(); auto sub = ui->mdiArea->addSubWindow(view); sub->setWindowTitle(url.toString()); QString image = SaoriApplication::saori()->cache()->fileCache(url); if (image.mid(0,2) == ":/") connect(SaoriApplication::saori()->cache(),&SaoriCache::downloaded,this,[=](const QUrl i){ if (i.toString() == url.toString()) { view->setPixmap(SaoriApplication::saori()->cache()->fileCache(url)); sub->resize(view->pixmap()->size()); } }); view->setPixmap(image); view->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding); sub->show(); sub->resize(view->pixmap()->size()); } void SaoriWindow::updateTimelineList() { ui->timelineTree->clear(); auto *accountItem = new QTreeWidgetItem(ui->timelineTree,QStringList() << tr("Accounts")); accountItem->setIcon(0,QIcon(":/icons/ionicons/person-stalker.svg")); for (int i = 0;i < SaoriApplication::getAccountList()->count();i ++) { auto *item = new QTreeWidgetItem(accountItem,QStringList() << SaoriApplication::getAccountList()->at(i)->name()); item->setIcon(0,QIcon(":/icons/ionicons/person.svg")); accountItem->addChild(item); for (auto tl:SaoriApplication::getAccountList()->at(i)->timelineList()) { auto title = getTimelineTitle(tl); auto tlItem = new QTreeWidgetItem(item,QStringList() << title.first << SaoriApplication::getAccountList()->at(i)->name() << tl); if (!title.second.isEmpty()) tlItem->setIcon(0,QIcon(title.second)); item->addChild(tlItem); } } auto *instanceItem = new QTreeWidgetItem(ui->timelineTree,QStringList() << tr("Instances")); instanceItem->setIcon(0,QIcon(":/icons/ionicons/earth.svg")); for (int i = 0;i < SaoriApplication::getInstanceList()->count();i ++) { auto *item = new QTreeWidgetItem(instanceItem,QStringList() << SaoriApplication::getInstanceList()->at(i)->instance().toString()); item->setIcon(0,QIcon(":/icons/ionicons/earth.svg")); instanceItem->addChild(item); } } void SaoriWindow::on_timelineTree_itemDoubleClicked(QTreeWidgetItem *item, int) { auto account = SaoriApplication::findAccount(item->text(1)); if (account) { openView(QStringList() << item->text(1) << item->text(2)); return; } auto instance = SaoriApplication::findInstance(QUrl(item->text(0))); if (instance) { openView(QStringList() << instance->instance().toString() << "instance"); return; } } void SaoriWindow::on_actionTabbedView_mode_toggled(bool arg1) { if (ui->mdiArea->viewMode() == QMdiArea::TabbedView) { if (arg1 == false) { ui->mdiArea->setViewMode(QMdiArea::SubWindowView); ui->actionTiled->setEnabled(true); } } else { if (arg1 == true) { ui->mdiArea->setViewMode(QMdiArea::TabbedView); ui->actionTiled->setEnabled(false); } } return; } void SaoriWindow::on_actionConfigure_triggered() { m_configDialog->show(); m_configDialog->raise(); m_configDialog->activateWindow(); }