#include #include #include "welcomedialog.h" #include "ui_welcomedialog.h" #include "recentprojectwidget.h" namespace Ember { WelcomeDialog::WelcomeDialog(ProjectManager* projectManager, QWidget *parent) : QDialog(parent), ui(new Ui::WelcomeDialog), m_projectManager(projectManager), m_originalPicture() { ui->setupUi(this); m_originalPicture = QPixmap(*ui->picture->pixmap()); setResult(Rejected); setupActions(); populateProjects(); } WelcomeDialog::~WelcomeDialog() { delete ui; } void WelcomeDialog::setupActions() { connect(ui->buttonNewProject, &QCommandLinkButton::clicked, this, &WelcomeDialog::onNewProjectClicked); connect(ui->buttonOpenProject, &QCommandLinkButton::clicked, this, &WelcomeDialog::onOpenProjectClicked); connect(ui->listRecentProjects, &QListWidget::currentItemChanged, this, &WelcomeDialog::onProjectCurrentChanged); connect(ui->listRecentProjects, &QListWidget::itemActivated, this, &WelcomeDialog::onProjectActivated); } void WelcomeDialog::populateProjects() { std::vector projects; for (auto pair : m_projectManager->recentProjects()) projects.push_back(pair.second); // Sort by pinned and access date std::sort(projects.begin(), projects.end(), [](const RecentProject& a, const RecentProject& b) -> bool { if (a.pinned == b.pinned) return a.access > b.access; return a.pinned > b.pinned; }); // Add items for (auto& project : projects) { RecentProjectWidget* itemWidget = new RecentProjectWidget(project); connect(itemWidget, &RecentProjectWidget::pinToggled, this, &WelcomeDialog::onProjectPinned); connect(itemWidget, &RecentProjectWidget::deleted, this, &WelcomeDialog::onProjectDeleted); QListWidgetItem* item = new QListWidgetItem(); item->setSizeHint(itemWidget->sizeHint()); ui->listRecentProjects->addItem(item); ui->listRecentProjects->setItemWidget(item, itemWidget); } } void WelcomeDialog::resizeEvent(QResizeEvent*) { QSize pictSize = ui->picture->size(); QPixmap pictScaled = m_originalPicture.scaled(pictSize, Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation); ui->picture->setPixmap(pictScaled); } void WelcomeDialog::onNewProjectClicked() { m_resultAction = NEW_PROJECT; accept(); } void WelcomeDialog::onOpenProjectClicked() { QString path = QFileDialog::getOpenFileName(this, tr("Open project"), "", buildFilterString()); if (!path.isEmpty()) { QByteArray str = path.toUtf8(); m_openPath.assign(str.begin(), str.end()); m_resultAction = OPEN_PROJECT; accept(); } } void WelcomeDialog::onProjectPinned(const RecentProject& project, bool pinned) { m_projectManager->pinRecentProject(project.path, pinned); } void WelcomeDialog::onProjectDeleted(const RecentProject& project) { m_projectManager->deleteRecentProject(project.path); delete ui->listRecentProjects->takeItem(ui->listRecentProjects->currentRow()); } void WelcomeDialog::onProjectCurrentChanged(QListWidgetItem *current, QListWidgetItem *previous) { if (previous) { RecentProjectWidget* itemWidget = (RecentProjectWidget*) ui->listRecentProjects->itemWidget(previous); if (itemWidget) itemWidget->setSelected(false); } if (current) { RecentProjectWidget* itemWidget = (RecentProjectWidget*) ui->listRecentProjects->itemWidget(current); if (itemWidget) itemWidget->setSelected(true); } } void WelcomeDialog::onProjectActivated(QListWidgetItem *item) { RecentProjectWidget* itemWidget = (RecentProjectWidget*) ui->listRecentProjects->itemWidget(item); if (itemWidget) { m_resultAction = OPEN_PROJECT; m_openPath = itemWidget->project().path; accept(); } } QString WelcomeDialog::buildFilterString() { static QString str; if (str.isEmpty()) { str = QString("%1 (*%2);;%3 (*.*)").arg( tr("Project files"), EMBER_PROJECT_EXTENSION, tr("All files")); } return str; } boost::filesystem::path WelcomeDialog::openPath() const { return m_openPath; } WelcomeDialog::ResultAction WelcomeDialog::resultAction() const { return m_resultAction; } }