From e27fb34908a62087b51eef574b7ceeee0439fc40 Mon Sep 17 00:00:00 2001 From: Rafael Date: Tue, 12 Apr 2016 20:56:58 +0200 Subject: [PATCH] PictureDialog navigating with arrow keys --- PictureDialog.cpp | 69 +++++++++++++++++++++++++++++++++++++++++++- PictureDialog.h | 17 +++++++++++ ProfileInterface.cpp | 69 ++++++++++++++++++++++++++++++++++++++++++++ ProfileInterface.h | 2 ++ SnapmaticWidget.cpp | 17 +++++++++++ SnapmaticWidget.h | 5 ++++ main.cpp | 2 +- 7 files changed, 179 insertions(+), 2 deletions(-) diff --git a/PictureDialog.cpp b/PictureDialog.cpp index eac181e..6cc14a1 100755 --- a/PictureDialog.cpp +++ b/PictureDialog.cpp @@ -32,6 +32,7 @@ #include #include #include +#include #include #include #include @@ -46,6 +47,7 @@ PictureDialog::PictureDialog(ProfileDatabase *profileDB, QWidget *parent) : jsonDrawString = ui->labJSON->text(); ui->cmdExport->setEnabled(0); plyrsList = QStringList(); + indexed = 0; picTitl = ""; picPath = ""; crewID = ""; @@ -59,6 +61,8 @@ PictureDialog::PictureDialog(ProfileDatabase *profileDB, QWidget *parent) : exportMenu->addAction(tr("Export as &JPG picture..."), this, SLOT(exportSnapmaticPicture())); exportMenu->addAction(tr("Export as >A Snapmatic..."), this, SLOT(copySnapmaticPicture())); ui->cmdExport->setMenu(exportMenu); + + installEventFilter(this); } PictureDialog::~PictureDialog() @@ -67,8 +71,31 @@ PictureDialog::~PictureDialog() delete ui; } -void PictureDialog::setSnapmaticPicture(SnapmaticPicture *picture, QString picturePath, bool readOk) +bool PictureDialog::eventFilter(QObject *obj, QEvent *ev) { + if (obj == this) + { + if (ev->type() == QEvent::KeyPress) + { + QKeyEvent *keyEvent = (QKeyEvent*)ev; + switch (keyEvent->key()){ + case Qt::Key_Left: + emit previousPictureRequested(); + break; + case Qt::Key_Right: + emit nextPictureRequested(); + break; + } + } + } + return false; +} + +void PictureDialog::setSnapmaticPicture(SnapmaticPicture *picture, QString picturePath, bool readOk, bool _indexed, int _index) +{ + indexed = _indexed; + index = _index; + // Showing error if reading error QImage snapmaticPicture; picPath = picturePath; @@ -127,6 +154,36 @@ void PictureDialog::setSnapmaticPicture(SnapmaticPicture *picture, QString pictu } } +void PictureDialog::setSnapmaticPicture(SnapmaticPicture *picture, QString picPath, bool readOk) +{ + setSnapmaticPicture(picture, picPath, readOk, false, 0); +} + +void PictureDialog::setSnapmaticPicture(SnapmaticPicture *picture, QString picPath) +{ + setSnapmaticPicture(picture, picPath, true); +} + +void PictureDialog::setSnapmaticPicture(SnapmaticPicture *picture, bool readOk, int index) +{ + setSnapmaticPicture(picture, picture->getPictureFileName(), readOk, true, index); +} + +void PictureDialog::setSnapmaticPicture(SnapmaticPicture *picture, bool readOk) +{ + setSnapmaticPicture(picture, picture->getPictureFileName(), readOk); +} + +void PictureDialog::setSnapmaticPicture(SnapmaticPicture *picture, int index) +{ + setSnapmaticPicture(picture, true, index); +} + +void PictureDialog::setSnapmaticPicture(SnapmaticPicture *picture) +{ + setSnapmaticPicture(picture, true); +} + void PictureDialog::playerNameUpdated() { if (plyrsList.count() >= 1) @@ -196,3 +253,13 @@ void PictureDialog::on_labPicture_mouseDoubleClicked() pictureWidget->deleteLater(); delete pictureWidget; } + +bool PictureDialog::isIndexed() +{ + return indexed; +} + +int PictureDialog::getIndex() +{ + return index; +} diff --git a/PictureDialog.h b/PictureDialog.h index 5b041a7..b85103e 100755 --- a/PictureDialog.h +++ b/PictureDialog.h @@ -33,7 +33,15 @@ class PictureDialog : public QDialog Q_OBJECT public: explicit PictureDialog(ProfileDatabase *profileDB, QWidget *parent = 0); + void setSnapmaticPicture(SnapmaticPicture *picture, QString picPath, bool readOk, bool indexed, int index); void setSnapmaticPicture(SnapmaticPicture *picture, QString picPath, bool readOk); + void setSnapmaticPicture(SnapmaticPicture *picture, QString picPath); + void setSnapmaticPicture(SnapmaticPicture *picture, bool readOk, int index); + void setSnapmaticPicture(SnapmaticPicture *picture, bool readOk); + void setSnapmaticPicture(SnapmaticPicture *picture, int index); + void setSnapmaticPicture(SnapmaticPicture *picture); + bool isIndexed(); + int getIndex(); ~PictureDialog(); public slots: @@ -44,6 +52,13 @@ private slots: void exportSnapmaticPicture(); void on_labPicture_mouseDoubleClicked(); +signals: + void nextPictureRequested(); + void previousPictureRequested(); + +protected: + bool eventFilter(QObject *obj, QEvent *ev); + private: ProfileDatabase *profileDB; Ui::PictureDialog *ui; @@ -57,6 +72,8 @@ private: QString locX; QString locY; QString locZ; + bool indexed; + int index; QMenu *exportMenu; }; diff --git a/ProfileInterface.cpp b/ProfileInterface.cpp index a83fb6e..e68edc6 100755 --- a/ProfileInterface.cpp +++ b/ProfileInterface.cpp @@ -22,6 +22,7 @@ #include "SnapmaticWidget.h" #include "DatabaseThread.h" #include "SavegameWidget.h" +#include "PictureDialog.h" #include "PictureExport.h" #include "StandardPaths.h" #include "ProfileLoader.h" @@ -143,6 +144,8 @@ void ProfileInterface::pictureLoaded_f(SnapmaticPicture *picture, QString pictur QObject::connect(picWidget, SIGNAL(widgetDeselected()), this, SLOT(profileWidgetDeselected())); QObject::connect(picWidget, SIGNAL(allWidgetsSelected()), this, SLOT(selectAllWidgets())); QObject::connect(picWidget, SIGNAL(allWidgetsDeselected()), this, SLOT(deselectAllWidgets())); + QObject::connect(picWidget, SIGNAL(nextPictureRequested(QWidget*)), this, SLOT(dialogNextPictureRequested(QWidget*))); + QObject::connect(picWidget, SIGNAL(previousPictureRequested(QWidget*)), this, SLOT(dialogPreviousPictureRequested(QWidget*))); if (inserted) { insertSnapmaticIPI(picWidget); } } @@ -191,6 +194,72 @@ void ProfileInterface::insertSavegameIPI(QWidget *widget) } } +void ProfileInterface::dialogNextPictureRequested(QWidget *dialog) +{ + PictureDialog *picDialog = (PictureDialog*)dialog; + ProfileWidget *proWidget = (ProfileWidget*)sender(); + if (widgets.contains(proWidget)) + { + QString widgetKey = widgets[proWidget]; + QStringList widgetsKeyList = widgets.values(); + QStringList pictureKeyList = widgetsKeyList.filter("PIC", Qt::CaseSensitive); +#if QT_VERSION >= 0x050600 + qSort(pictureKeyList.rbegin(), pictureKeyList.rend()); +#else + qSort(pictureKeyList.begin(), pictureKeyList.end(), qGreater()); +#endif + int picIndex; + if (picDialog->isIndexed()) + { + picIndex = picDialog->getIndex(); + } + else + { + picIndex = pictureKeyList.indexOf(QRegExp(widgetKey)); + } + picIndex++; + if (pictureKeyList.length() > picIndex) + { + QString newWidgetKey = pictureKeyList.at(picIndex); + SnapmaticWidget *picWidget = (SnapmaticWidget*)widgets.key(newWidgetKey); + picDialog->setSnapmaticPicture(picWidget->getPicture(), picIndex); + } + } +} + +void ProfileInterface::dialogPreviousPictureRequested(QWidget *dialog) +{ + PictureDialog *picDialog = (PictureDialog*)dialog; + ProfileWidget *proWidget = (ProfileWidget*)sender(); + if (widgets.contains(proWidget)) + { + QString widgetKey = widgets[proWidget]; + QStringList widgetsKeyList = widgets.values(); + QStringList pictureKeyList = widgetsKeyList.filter("PIC", Qt::CaseSensitive); +#if QT_VERSION >= 0x050600 + qSort(pictureKeyList.rbegin(), pictureKeyList.rend()); +#else + qSort(pictureKeyList.begin(), pictureKeyList.end(), qGreater()); +#endif + int picIndex; + if (picDialog->isIndexed()) + { + picIndex = picDialog->getIndex(); + } + else + { + picIndex = pictureKeyList.indexOf(QRegExp(widgetKey)); + } + if (picIndex > 0) + { + picIndex--; + QString newWidgetKey = pictureKeyList.at(picIndex ); + SnapmaticWidget *picWidget = (SnapmaticWidget*)widgets.key(newWidgetKey); + picDialog->setSnapmaticPicture(picWidget->getPicture(), picIndex); + } + } +} + void ProfileInterface::sortingProfileInterface() { ui->vlSavegame->setEnabled(false); diff --git a/ProfileInterface.h b/ProfileInterface.h index b1bf190..7349d6c 100755 --- a/ProfileInterface.h +++ b/ProfileInterface.h @@ -65,6 +65,8 @@ private slots: void profileLoaded_p(); void profileWidgetSelected(); void profileWidgetDeselected(); + void dialogNextPictureRequested(QWidget *dialog); + void dialogPreviousPictureRequested(QWidget *dialog); private: ProfileDatabase *profileDB; diff --git a/SnapmaticWidget.cpp b/SnapmaticWidget.cpp index 0175562..e6f146f 100755 --- a/SnapmaticWidget.cpp +++ b/SnapmaticWidget.cpp @@ -89,6 +89,11 @@ void SnapmaticWidget::setSnapmaticPicture(SnapmaticPicture *picture, QString pic ui->labPicture->setPixmap(SnapmaticPixmap); } +void SnapmaticWidget::setSnapmaticPicture(SnapmaticPicture *picture) +{ + setSnapmaticPicture(picture, picture->getPictureFileName()); +} + void SnapmaticWidget::on_cmdView_clicked() { PictureDialog *picDialog = new PictureDialog(profileDB, this); @@ -98,6 +103,8 @@ void SnapmaticWidget::on_cmdView_clicked() // be ready for playerName updated QObject::connect(threadDB, SIGNAL(playerNameUpdated()), picDialog, SLOT(playerNameUpdated())); + QObject::connect(picDialog, SIGNAL(nextPictureRequested()), this, SLOT(dialogNextPictureRequested())); + QObject::connect(picDialog, SIGNAL(previousPictureRequested()), this, SLOT(dialogPreviousPictureRequested())); // show picture dialog picDialog->showNormal(); @@ -220,6 +227,16 @@ void SnapmaticWidget::contextMenuEvent(QContextMenuEvent *ev) //ui->SnapmaticFrame->setStyleSheet(""); } +void SnapmaticWidget::dialogNextPictureRequested() +{ + emit nextPictureRequested((QWidget*)sender()); +} + +void SnapmaticWidget::dialogPreviousPictureRequested() +{ + emit previousPictureRequested((QWidget*)sender()); +} + void SnapmaticWidget::on_cbSelected_stateChanged(int arg1) { if (arg1 == Qt::Checked) diff --git a/SnapmaticWidget.h b/SnapmaticWidget.h index a469ccd..76ed809 100755 --- a/SnapmaticWidget.h +++ b/SnapmaticWidget.h @@ -40,6 +40,7 @@ class SnapmaticWidget : public ProfileWidget public: SnapmaticWidget(ProfileDatabase *profileDB, DatabaseThread *threadDB, QWidget *parent = 0); void setSnapmaticPicture(SnapmaticPicture *picture, QString picturePath); + void setSnapmaticPicture(SnapmaticPicture *picture); void setSelectionMode(bool selectionMode); void setSelected(bool isSelected); SnapmaticPicture *getPicture(); @@ -57,6 +58,8 @@ private slots: void pictureSelected(); void selectAllWidgets(); void deselectAllWidgets(); + void dialogNextPictureRequested(); + void dialogPreviousPictureRequested(); protected: bool eventFilter(QObject *obj, QEvent *ev); @@ -83,6 +86,8 @@ signals: void widgetDeselected(); void allWidgetsSelected(); void allWidgetsDeselected(); + void nextPictureRequested(QWidget *dialog); + void previousPictureRequested(QWidget *dialog); }; #endif // SNAPMATICWIDGET_H diff --git a/main.cpp b/main.cpp index a2e6aa1..d8b0607 100755 --- a/main.cpp +++ b/main.cpp @@ -412,7 +412,7 @@ int main(int argc, char *argv[]) bool readOk = picture.readingPictureFromFile(arg1); picDialog->setWindowFlags(picDialog->windowFlags()^Qt::WindowContextHelpButtonHint); picDialog->setWindowIcon(IconLoader::loadingAppIcon()); - picDialog->setSnapmaticPicture(&picture, arg1, readOk); + picDialog->setSnapmaticPicture(&picture, readOk); int crewID = picture.getCrewNumber(); if (crewID != 0) { crewDB->addCrew(crewID); }