view src/mainwindow.cpp @ 102:568d92c03941

Auto: Update translations
author Andre Heinecke <andre.heinecke@intevation.de>
date Fri, 18 Nov 2016 12:47:11 +0100
parents ad78dc946ed3
children
line wrap: on
line source
/* Copyright (C) 2015 by ETH Zürich
 * Software engineering by Intevation GmbH
 *
 * This file is Free Software under the GNU GPL (v>=2)
 * and comes with ABSOLUTELY NO WARRANTY!
 * See LICENSE.txt for details.
 */

/** @file See mainwindow.h */
#include "mainwindow.h"

#include "constants.h"
#include "metadataview.h"
#include "pngplayer.h"
#include "folderselectdialog.h"

#include <QDebug>
#include <QDialog>
#include <QWidget>
#include <QHBoxLayout>
#include <QLabel>
#include <QPushButton>
#include <QDir>
#include <QFileDialog>
#include <QMessageBox>
#include <QApplication>
#include <QDragEnterEvent>
#include <QDragLeaveEvent>
#include <QDropEvent>
#include <QDragMoveEvent>
#include <QMimeData>
#include <QStandardPaths>
#include <QStyle>
#include <QStatusBar>
#include <QTabWidget>
#include <QVBoxLayout>
#include <QShortcut>
#include <QKeySequence>

MainWindow::MainWindow() :
    mDataView(NULL) {
    setStatusBar(new QStatusBar());

    setupGUI();
    readSettings();
    setAcceptDrops(true);
    setupShortcuts();
#ifndef Q_OS_WIN
    QIcon windowIcon = QIcon(":/icon-64.png");
    setWindowIcon(windowIcon);
#endif
}

void MainWindow::setupGUI() {
    QTabWidget *tabWidget = new QTabWidget;
    mDataView = new MetaDataView(this);
    mPlayer = new PNGPlayer(this);

    tabWidget->addTab(mPlayer, tr("Player"));
    tabWidget->addTab(mDataView, tr("Filter/Details"));
    tabWidget->setMovable(true);

    setCentralWidget(tabWidget);

    QPushButton *backBtn = new QPushButton;
    backBtn->setIcon(QApplication::style()->standardIcon(QStyle::SP_ArrowBack));
    backBtn->setToolTip(tr("Back to exam selection."));
    tabWidget->setCornerWidget(backBtn, Qt::TopLeftCorner);

    QLabel *versionLabel = new QLabel("<small>" + tr("Version: %1").arg(VERSION) + "</small>");
    statusBar()->insertPermanentWidget(0, versionLabel);

    connect (backBtn, &QPushButton::clicked, this, &MainWindow::goBack);
    connect (mPlayer, &PNGPlayer::error, this, &MainWindow::showErrorMessage);
    connect (mPlayer, &PNGPlayer::advance, mDataView, &MetaDataView::selectNextRow);
    connect (mPlayer, &PNGPlayer::back, mDataView, &MetaDataView::selectPrevRow);
    connect (mPlayer, &PNGPlayer::jumpToFrame, mDataView, &MetaDataView::selectRow);
    connect (mDataView, &MetaDataView::selectionChanged, mPlayer,
             &PNGPlayer::showPicture);
    connect (mDataView, &MetaDataView::selectionChanged, this,
             &MainWindow::showPictureNameStatus);
}

void MainWindow::showErrorMessage(const QString& errMsg) {
    QMessageBox::warning(this, tr("Error!"), errMsg);
    statusBar()->showMessage(errMsg);
}

void MainWindow::closeEvent(QCloseEvent *event) {
    mSettings.setValue("geometry", saveGeometry());
    mSettings.setValue("windowState", saveState());
    mPlayer->close();
    QMainWindow::closeEvent(event);
}

void MainWindow::readSettings() {
    if (mSettings.contains("geometry")) {
        restoreGeometry(mSettings.value("geometry").toByteArray());
        restoreState(mSettings.value("windowState").toByteArray());
    }
}

void MainWindow::setFolder(const QString& folder)
{
    QFileInfo fi(folder);
    mSettings.setValue(SUB_FOLDER_KEY, folder);
    if (!fi.isDir() || !fi.isReadable()) {
        showErrorMessage(tr("Failed to access directory: '%1'").arg(folder));
    }
    QDir dir = QDir(folder);
    QFileInfo metaData = dir.absoluteFilePath("metadata.csv");
    if (!metaData.exists() || !metaData.isReadable()) {
        showErrorMessage(tr("Failed to access meta data file: '%1'").arg(
                    metaData.filePath()));

    }
    QString errorMsg = mDataView->parseMetaData(metaData.filePath());
    if (!errorMsg.isEmpty()) {
        showErrorMessage(errorMsg);
    }
    statusBar()->showMessage(tr("Parsed: '%1'").arg(metaData.filePath()));
    qDebug() << "Parsed: " << metaData.filePath();
    mCurFolder = folder;
    mPlayer->setBaseDir(folder);
    mDataView->selectNextRow();
    if (!mSettings.contains("geometry")) {
        qDebug() << "Showing maximized";
        show();
        showMaximized();
    } else {
        show();
    }
}

void MainWindow::showPictureNameStatus(const QString& fileName, int current,
            int max, const QString& timestamp) {
    if (current != 0 && max != 0) {
        statusBar()->showMessage(tr("Showing: '%1'").arg(fileName));
    }
}

void MainWindow::setRootFolder(const QString &folder) {
    mRootFolder = folder;
    QDir dir(folder);
    dir.cdUp();
    mSettings.setValue(ROOT_FOLDER_KEY, dir.absolutePath());

    showSubFolderSelect();
}

void MainWindow::showSubFolderSelect() {
    const QString subPattern = mSettings.value(SUB_FOLDER_PATTERN_KEY,
                                               SUB_FOLDER_PATTERN).toString();
    mSettings.setValue(SUB_FOLDER_PATTERN_KEY, subPattern);
    FolderSelectDialog *fsDialog = new FolderSelectDialog(mRootFolder,
                                                          subPattern);
    fsDialog->setWindowTitle(tr("Persons"));
    connect(fsDialog, &FolderSelectDialog::folderSelected,
            this, &MainWindow::setFolder);

    connect(fsDialog, &FolderSelectDialog::backClicked,
            this, &MainWindow::showWithFolderSelect);
    connect(fsDialog, &FolderSelectDialog::backClicked,
            fsDialog, &QDialog::close);
    fsDialog->show();
}

void MainWindow::showWithFolderSelect() {
    const QString startFolder = mSettings.value(ROOT_FOLDER_KEY,
                                                QString()).toString();
    const QString rootPattern = mSettings.value(ROOT_FOLDER_PATTERN_KEY,
                                                DEFAULT_ROOT_PATTERN).toString();
    mSettings.setValue(ROOT_FOLDER_PATTERN_KEY, rootPattern);
    FolderSelectDialog *fsDialog = new FolderSelectDialog(startFolder,
                                                          rootPattern,
                                                          tr("Root-Path"));
    fsDialog->setWindowTitle(tr("Exams"));
    connect(fsDialog, &FolderSelectDialog::folderSelected,
            this, &MainWindow::setRootFolder);

    fsDialog->show();
   // connect(fsDialog, &QDialog::closeEvent, fsDialog, &QObject::deleteLater);
}

void MainWindow::setupShortcuts() {
    QShortcut *next = new QShortcut(Qt::Key_Right, this);
    QShortcut *prev = new QShortcut(Qt::Key_Left, this);
    QShortcut *pause = new QShortcut(Qt::Key_Space, this);

    connect(pause, &QShortcut::activated, mPlayer, &PNGPlayer::togglePlay);
    connect(next, &QShortcut::activated, mDataView, &MetaDataView::selectNextRow);
    connect(prev, &QShortcut::activated, mDataView, &MetaDataView::selectPrevRow);
}

void MainWindow::goBack() {
    hide();
    showSubFolderSelect();
}
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)