view ui/administratorwindow.cpp @ 382:124f08a85532

Rename instructions to choices
author Andre Heinecke <andre.heinecke@intevation.de>
date Tue, 15 Apr 2014 17:46:16 +0200
parents 9731d28b95af
children 7e0a188d6848
line wrap: on
line source
#include "administratorwindow.h"

#include <QDebug>
#include <QMessageBox>
#include <QAction>
#include <QMenu>
#include <QApplication>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QGroupBox>
#include <QSplitter>
#include <QLabel>
#include <QImage>
#include <QCheckBox>
#include <QHeaderView>
#include <QFileDialog>

#include "certificatetabledelegate.h"
#include "createinstallerdialog.h"
#include "createcertlistdialog.h"

AdministratorWindow::AdministratorWindow() {
    createActions();
    createMenuBar();
    createContent();
}

void AdministratorWindow::createActions()
{
}

void AdministratorWindow::createMenuBar()
{
    menuBar = new QMenuBar(this);
    QMenu *menu = new QMenu(tr("Menu"), menuBar);
    menuBar->addMenu(menu);
    QAction *createInstaller = menu->addAction(tr("Create Installer"));
    QAction *settings = menu->addAction(tr("Settings"));
    menu->addSeparator();
    QAction *help = menu->addAction(tr("Help"));
    QAction *about = menu->addAction(tr("About"));
    menu->addSeparator();
    QAction *quit = menu->addAction(tr("Quit"));
    connect(createInstaller, SIGNAL(triggered()), this, SLOT(createInstaller()));
    connect(settings, SIGNAL(triggered()), this, SLOT(showSettings()));
    connect(help, SIGNAL(triggered()), this, SLOT(showHelp()));
    connect(about, SIGNAL(triggered()), this, SLOT(showAbout()));
    connect(quit, SIGNAL(triggered()), qApp, SLOT(quit()));
    setMenuBar(menuBar);
}

void AdministratorWindow::createContent()
{
    // Create a central widget containing the main layout.
    QWidget *base = new QWidget;

    // Layouts and Container
    QVBoxLayout *mainLayout = new QVBoxLayout;
    QVBoxLayout *certLayout = new QVBoxLayout;
    QHBoxLayout *headerLayout = new QHBoxLayout;
    QVBoxLayout *headerTextLayout = new QVBoxLayout;
    QHBoxLayout *bottomLayout = new QHBoxLayout;

    // The certificate list
    QGroupBox *certBox = new QGroupBox(tr("Managed Certificates"));
    certificateView = new QTableView;
    certificateModel = new CertificateTabelModel();
    CertificateTableDelegate *delegate = new CertificateTableDelegate(certificateView);
    certificateView->setModel(certificateModel);
    certificateView->setItemDelegate(delegate);
    certificateView->setColumnWidth(0, 50);
    certificateView->setColumnWidth(2, 130);
    certificateView->setColumnWidth(3, 130);
    certificateView->setColumnWidth(4, 200);
    certificateView->setSelectionBehavior(QAbstractItemView::SelectRows);
    connect(certificateView, SIGNAL(clicked(const QModelIndex&)), this,
        SLOT(clickedCertificate(const QModelIndex&)));
    certificateView->verticalHeader()->setVisible(false);
    certLayout->addWidget(certificateView);
    certBox->setLayout(certLayout);

    // The header (icon, about text)
    QImage *logoImage = new QImage(":/img/logo.png");
    QLabel *logo = new QLabel;
    logo->setBackgroundRole(QPalette::Base);
    logo->setPixmap(QPixmap::fromImage(*logoImage));
    QLabel *title = new QLabel("<h2>" + tr("Administrator Application") + "</h2>");
    QLabel *subTitle = new QLabel("This Software creates a signed file containing certificates");
    headerTextLayout->addWidget(title);
    headerTextLayout->addWidget(subTitle);
    headerLayout->addWidget(logo);
    headerLayout->addLayout(headerTextLayout);
    headerLayout->setStretch(0, 0);
    headerLayout->setStretch(1, 10);

    // The buttons.
    bottomLayout->setAlignment(Qt::AlignBottom);
    saveButton = new QPushButton(tr("Save"));
    connect(saveButton, SIGNAL(clicked()), this, SLOT(saveCertificateFile()));
    loadButton = new QPushButton(tr("Load"));
    connect(loadButton, SIGNAL(clicked()), this, SLOT(loadCertificateFile()));
    addButton = new QPushButton(tr("Add"));
    connect(addButton, SIGNAL(clicked()), this, SLOT(addCertificates()));
    removeButton = new QPushButton(tr("Remove"));
    removeButton->setEnabled(false);
    connect(removeButton, SIGNAL(clicked()), this, SLOT(removeCertificates()));
    bottomLayout->addWidget(saveButton);
    bottomLayout->addWidget(loadButton);
    bottomLayout->addWidget(addButton);
    bottomLayout->addWidget(removeButton);
    bottomLayout->insertStretch(4, 10);

    mainLayout->addLayout(headerLayout);
    mainLayout->addWidget(certBox);
    mainLayout->addLayout(bottomLayout);


    // QMainWindow allready has a layout. All child layouts and widgets are
    // managed in the central widget.
    base->setLayout(mainLayout);
    setCentralWidget(base);
    certificateView->horizontalHeader()->setSectionResizeMode(1,
        QHeaderView::Stretch);
}

void AdministratorWindow::loadCertificateFile()
{
    QString certFile = QFileDialog::getOpenFileName(
        this, tr("Select certificate file"), "/home/rrenkert/local-home/projects/m13/src/repo/ui/tests/data/", "*.txt");
    qDebug() << "selected: " + certFile;
    certList.readList(certFile.toLocal8Bit().constData());
    if (!certList.isValid()) {
        qDebug() << "Not a valid list.";
    }
    else {
        loadCertificateTable();
    }
}

void AdministratorWindow::saveCertificateFile()
{
    CreateCertListDialog *dialog = new CreateCertListDialog(this);
    dialog->show();
}

void AdministratorWindow::addCertificates()
{
    QString certFile = QFileDialog::getOpenFileName(
        this, tr("Select certificate"), "/home/rrenkert/local-home/projects/m13/src/repo/ui/tests/data/", "*.pem *.der");
    QList<Certificate> certs = Certificate::fromFileName(certFile);
    addToCertificateTable(certs);
}

void AdministratorWindow::removeCertificates()
{
    QModelIndexList list = certificateView->selectionModel()->selectedRows();
    for (int i = list.size() -1 ; i >= 0; i--) {
        certificateModel->removeRow(list.at(i).row(), list.at(i));
    }
}

void AdministratorWindow::loadCertificateTable() {
    foreach(const Certificate &cert, certList.getCertificates()) {
        certificateModel->addCertificate(cert, true);
    }
}

void AdministratorWindow::addToCertificateTable(const QList<Certificate> &certs)
{
    foreach(const Certificate &cert, certs) {
        certificateModel->addCertificate(cert, false);
    }
}

void AdministratorWindow::showSettings()
{
    qDebug() << "show settingsdialog";
}

void AdministratorWindow::showHelp()
{
    qDebug() << "show helpdialog";
}

void AdministratorWindow::showAbout()
{
    qDebug() << "show aboutdialog";
}

void AdministratorWindow::createInstaller()
{
    qDebug() << "create Installer";
    CreateInstallerDialog *dialog = new CreateInstallerDialog(this);
    dialog->show();
}

void AdministratorWindow::clickedCertificate(const QModelIndex &index)
{
    if (index.data(Qt::UserRole).toBool()) {
        removeButton->setEnabled(false);
    }
    else {
        removeButton->setEnabled(true);
    }
}

http://wald.intevation.org/projects/trustbridge/