From eaafc489fc625d71e4ce797cebb28056746896bc Mon Sep 17 00:00:00 2001 From: Tom Tanner Date: Thu, 9 Jul 2015 18:58:34 +0100 Subject: Reworking the Custom Executables list and the shortcut popup. Certainly fixes the issue where the 'toolbar' entry doesn't lose the cross. One of the 'find' methods ignored case but none of the others did, so I've made it not ignore case either. Also made the code to get the desktop/startmenu paths use a more up-to-date API --- src/mainwindow.cpp | 121 ++++++++++++++++++++++++++--------------------------- 1 file changed, 59 insertions(+), 62 deletions(-) (limited to 'src/mainwindow.cpp') diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 4ffa044b..31596fed 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -489,9 +489,6 @@ void MainWindow::updateToolBar() QAction *exeAction = new QAction(iconForExecutable(iter->m_BinaryInfo.filePath()), iter->m_Title, ui->toolBar); - QVariant temp; - temp.setValue(*iter); - exeAction->setData(temp); exeAction->setObjectName(QString("custom__") + iter->m_Title); if (!connect(exeAction, SIGNAL(triggered()), this, SLOT(startExeAction()))) { qDebug("failed to connect trigger?"); @@ -955,7 +952,7 @@ void MainWindow::startExeAction() { QAction *action = qobject_cast(sender()); if (action != nullptr) { - Executable selectedExecutable = action->data().value(); + Executable const &selectedExecutable(m_OrganizerCore.executablesList()->find(action->text())); m_OrganizerCore.spawnBinary( selectedExecutable.m_BinaryInfo, selectedExecutable.m_Arguments, @@ -1198,10 +1195,8 @@ void MainWindow::refreshExecutablesList() std::vector::const_iterator current, end; m_OrganizerCore.executablesList()->getExecutables(current, end); for(int i = 0; current != end; ++current, ++i) { - QVariant temp; - temp.setValue(*current); QIcon icon = iconForExecutable(current->m_BinaryInfo.filePath()); - executablesList->addItem(icon, current->m_Title, temp); + executablesList->addItem(icon, current->m_Title); model->setData(model->index(i, 0), QSize(0, executablesList->iconSize().height() + 4), Qt::SizeHintRole); } @@ -1608,9 +1603,7 @@ void MainWindow::installMod(QString fileName) void MainWindow::on_startButton_clicked() { - QComboBox* executablesList = findChild("executablesListBox"); - - Executable selectedExecutable = executablesList->itemData(executablesList->currentIndex()).value(); + Executable const &selectedExecutable(getSelectedExecutable()); m_OrganizerCore.spawnBinary( selectedExecutable.m_BinaryInfo, @@ -1718,11 +1711,10 @@ void MainWindow::on_executablesListBox_currentIndexChanged(int index) m_OldExecutableIndex = index; if (executablesList->isEnabled()) { - - if (executablesList->itemData(index).isNull()) { + //I think the 2nd test is impossible + if (index == 0 || index > static_cast(m_OrganizerCore.executablesList()->size())) { if (modifyExecutablesDialog()) { setExecutableIndex(previousIndex); -// executablesList->setCurrentIndex(previousIndex); } } else { setExecutableIndex(index); @@ -1737,7 +1729,6 @@ void MainWindow::helpTriggered() void MainWindow::wikiTriggered() { -// ::ShellExecuteW(nullptr, L"open", L"http://issue.tannin.eu/tbg/wiki/Modorganizer%3AMainPage", nullptr, nullptr, SW_SHOWNORMAL); ::ShellExecuteW(nullptr, L"open", L"http://wiki.step-project.com/Guide:Mod_Organizer", nullptr, nullptr, SW_SHOWNORMAL); } @@ -3244,27 +3235,43 @@ void MainWindow::on_savegameList_customContextMenuRequested(const QPoint &pos) void MainWindow::linkToolbar() { - const Executable &selectedExecutable = ui->executablesListBox->itemData(ui->executablesListBox->currentIndex()).value(); - Executable &exe = m_OrganizerCore.executablesList()->find(selectedExecutable.m_Title); + Executable &exe(getSelectedExecutable()); exe.m_Toolbar = !exe.m_Toolbar; - ui->linkButton->menu()->actions().at(2)->setIcon(exe.m_Toolbar ? QIcon(":/MO/gui/remove") : QIcon(":/MO/gui/link")); + ui->linkButton->menu()->actions().at(static_cast(Shortcut_Type::Toolbar))->setIcon(exe.m_Toolbar ? QIcon(":/MO/gui/remove") : QIcon(":/MO/gui/link")); updateToolBar(); } -void MainWindow::linkDesktop() +namespace { +QString getLinkfile(QString const &dir, Executable const &exec) +{ + return QDir::fromNativeSeparators(dir) + "/" + exec.m_Title + ".lnk"; +} + +QString getDesktopLinkfile(Executable const &exec) { - const Executable &selectedExecutable = - ui->executablesListBox->itemData(ui->executablesListBox->currentIndex()).value(); - QString linkName = getDesktopDirectory() + "\\" + selectedExecutable.m_Title + ".lnk"; + return getLinkfile(getDesktopDirectory(), exec); +} + +QString getStartMenuLinkfile(Executable const &exec) +{ + return getLinkfile(getStartMenuDirectory(), exec); +} +} + +void MainWindow::addWindowsLink(Shortcut_Type const mapping) +{ + Executable const &selectedExecutable(getSelectedExecutable()); + QString const linkName = getLinkfile(mapping == Shortcut_Type::Windows_Desktop ? getDesktopDirectory() : getStartMenuDirectory(), + selectedExecutable); if (QFile::exists(linkName)) { if (QFile::remove(linkName)) { - ui->linkButton->menu()->actions().at(0)->setIcon(QIcon(":/MO/gui/link")); + ui->linkButton->menu()->actions().at(static_cast(mapping))->setIcon(QIcon(":/MO/gui/link")); } else { reportError(tr("failed to remove %1").arg(linkName)); } } else { - QFileInfo exeInfo(qApp->applicationFilePath()); + QFileInfo const exeInfo(qApp->applicationFilePath()); // create link std::wstring targetFile = ToWString(exeInfo.absoluteFilePath()); std::wstring parameter = ToWString(QString("\"%1\" %2").arg(QDir::toNativeSeparators(selectedExecutable.m_BinaryInfo.absoluteFilePath())) @@ -3274,46 +3281,24 @@ void MainWindow::linkDesktop() if (CreateShortcut(targetFile.c_str() , parameter.c_str() - , linkName.toUtf8().constData() + , QDir::toNativeSeparators(linkName).toUtf8().constData() , description.c_str() - , currentDirectory.c_str()) != E_INVALIDARG) { - ui->linkButton->menu()->actions().at(0)->setIcon(QIcon(":/MO/gui/remove")); + , currentDirectory.c_str()) == 0) { + ui->linkButton->menu()->actions().at(static_cast(mapping))->setIcon(QIcon(":/MO/gui/remove")); } else { reportError(tr("failed to create %1").arg(linkName)); } } } -void MainWindow::linkMenu() +void MainWindow::linkDesktop() { - QComboBox* executablesList = findChild("executablesListBox"); - - const Executable &selectedExecutable = executablesList->itemData(executablesList->currentIndex()).value(); - QString linkName = getStartMenuDirectory() + "\\" + selectedExecutable.m_Title + ".lnk"; - - if (QFile::exists(linkName)) { - if (QFile::remove(linkName)) { - ui->linkButton->menu()->actions().at(1)->setIcon(QIcon(":/MO/gui/link")); - } else { - reportError(tr("failed to remove %1").arg(linkName)); - } - } else { - QFileInfo exeInfo(qApp->applicationFilePath()); - // create link - std::wstring targetFile = ToWString(exeInfo.absoluteFilePath()); - std::wstring parameter = ToWString(QString("\"%1\" %2").arg(QDir::toNativeSeparators(selectedExecutable.m_BinaryInfo.absoluteFilePath())) - .arg(selectedExecutable.m_Arguments)); - std::wstring description = ToWString(selectedExecutable.m_BinaryInfo.fileName()); - std::wstring currentDirectory = ToWString(QDir::toNativeSeparators(exeInfo.absolutePath())); + addWindowsLink(Shortcut_Type::Windows_Desktop); +} - if (CreateShortcut(targetFile.c_str(), parameter.c_str(), - linkName.toUtf8().constData(), - description.c_str(), currentDirectory.c_str()) != E_INVALIDARG) { - ui->linkButton->menu()->actions().at(1)->setIcon(QIcon(":/MO/gui/remove")); - } else { - reportError(tr("failed to create %1").arg(linkName)); - } - } +void MainWindow::linkMenu() +{ + addWindowsLink(Shortcut_Type::Windows_StartMenu); } void MainWindow::on_actionSettings_triggered() @@ -4209,19 +4194,31 @@ void MainWindow::on_groupCombo_currentIndexChanged(int index) modFilterActive(m_ModListSortProxy->isFilterActive()); } +const Executable &MainWindow::getSelectedExecutable() const +{ + QString name = ui->executablesListBox->itemText(ui->executablesListBox->currentIndex()); + return m_OrganizerCore.executablesList()->find(name); +} + +Executable &MainWindow::getSelectedExecutable() +{ + QString name = ui->executablesListBox->itemText(ui->executablesListBox->currentIndex()); + return m_OrganizerCore.executablesList()->find(name); +} + void MainWindow::on_linkButton_pressed() { - const Executable &selectedExecutable = ui->executablesListBox->itemData(ui->executablesListBox->currentIndex()).value(); + Executable const &selectedExecutable(getSelectedExecutable()); - QIcon addIcon(":/MO/gui/link"); - QIcon removeIcon(":/MO/gui/remove"); + QIcon const addIcon(":/MO/gui/link"); + QIcon const removeIcon(":/MO/gui/remove"); - QFileInfo linkDesktopFile(QDir::fromNativeSeparators(getDesktopDirectory()) + "/" + selectedExecutable.m_Title + ".lnk"); - QFileInfo linkMenuFile(QDir::fromNativeSeparators(getStartMenuDirectory()) + "/" + selectedExecutable.m_Title + ".lnk"); + QFileInfo const linkDesktopFile(getDesktopLinkfile(selectedExecutable)); + QFileInfo const linkMenuFile(getStartMenuLinkfile(selectedExecutable)); - ui->linkButton->menu()->actions().at(0)->setIcon(selectedExecutable.m_Toolbar ? removeIcon : addIcon); - ui->linkButton->menu()->actions().at(1)->setIcon(linkDesktopFile.exists() ? removeIcon : addIcon); - ui->linkButton->menu()->actions().at(2)->setIcon(linkMenuFile.exists() ? removeIcon : addIcon); + ui->linkButton->menu()->actions().at(static_cast(Shortcut_Type::Toolbar))->setIcon(selectedExecutable.m_Toolbar ? removeIcon : addIcon); + ui->linkButton->menu()->actions().at(static_cast(Shortcut_Type::Windows_Desktop))->setIcon(linkDesktopFile.exists() ? removeIcon : addIcon); + ui->linkButton->menu()->actions().at(static_cast(Shortcut_Type::Windows_StartMenu))->setIcon(linkMenuFile.exists() ? removeIcon : addIcon); } void MainWindow::on_showHiddenBox_toggled(bool checked) -- cgit v1.3.1