qcross/qcrossedit/cmainwindow.cpp

209 lines
5.8 KiB
C++

//
// C++ Implementation: cmainwindow
//
// Author: Oliver Groß <z.o.gross@gmx.de>, (C) 2008
//
// Copyright: See COPYING file that comes with this distribution
//
#include <QApplication>
#include <QStatusBar>
#include <QTreeView>
#include <QMenuBar>
#include <QMenu>
#include <QMessageBox>
#include <QCloseEvent>
#include <QDir>
#include <QFileDialog>
#include <QInputDialog>
#include <libqnono/ccrosspackagemodel.h>
#include <libqnono/ccrosspackage.h>
#include "cmainwindow.h"
namespace qcrossedit {
using namespace libqnono;
//public:
CMainWindow::CMainWindow(QWidget *parent) :
QMainWindow(parent),
m_Unsaved(false),
m_Package(NULL)
{
setupUi();
fileNew();
}
CMainWindow::~CMainWindow() {
}
//protected:
void CMainWindow::setupUi() {
m_PicListView = new QTreeView();
m_PicListView->setModel(new CCrossPackageModel(this));
m_PicListView->setRootIsDecorated(false);
setCentralWidget(m_PicListView);
QMenu * menu;
//file menu
menu = menuBar()->addMenu("&File");
menu->addAction("&New", this, SLOT(fileNew()), Qt::CTRL + Qt::Key_N);
menu->addAction("&Open", this, SLOT(fileOpen()), Qt::CTRL + Qt::Key_O);
menu->addSeparator();
menu->addAction("&Save", this, SLOT(fileSave()), Qt::CTRL + Qt::Key_S);
menu->addAction("Save as...", this, SLOT(fileSaveAs()));
menu->addSeparator();
menu->addAction("&Quit", this, SLOT(close()), Qt::CTRL + Qt::Key_Q);
//edit menu
menu = menuBar()->addMenu("&Edit");
menu->addAction("&Create empty nonogram...", this, SLOT(editCreateEmpty()));
menu->addAction("Create nonogram from &picture...", this, SLOT(editCreateFromPicture()));
menu->addSeparator();
menu->addAction("&Rename nonogram", this, SLOT(editRename()));
menu->addAction("Set nonogram &time-out", this, SLOT(editSetTimeout()));
menu->addAction("&Delete nonogram", this, SLOT(editDelete()));
menu->addSeparator();
menu->addAction("Set Package&name...", this, SLOT(editSetPackageName()));
//help Menu
menu = menuBar()->addMenu("&Help");
menu->addAction("About QCrossEdit", this, SLOT(helpAbout()));
menu->addAction("About Qt", qApp, SLOT(aboutQt()));
}
bool CMainWindow::promtUnsaved() {
switch (QMessageBox::question(this,
tr("Unsaved changes"),
tr("Would you like to save your changes?"),
QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel)) {
case QMessageBox::Yes:
if (!fileSave())
return false;
case QMessageBox::No:
return true;
default:
return false;
}
}
void CMainWindow::closeEvent(QCloseEvent * event) {
if (m_Unsaved && !promtUnsaved())
event->ignore();
else
event->accept();
}
//protected slots:
void CMainWindow::fileNew() {
if (m_Unsaved && !promtUnsaved())
return;
m_Unsaved = false;
if (m_Package)
delete m_Package;
m_Package = new CCrossPackage();
m_Package->setName(tr("unknown"));
qobject_cast<CCrossPackageModel *>(m_PicListView->model())->setPackage(m_Package);
statusBar()->showMessage(tr("New package created."));
}
void CMainWindow::fileOpen() {
if (m_Unsaved && !promtUnsaved())
return;
m_Unsaved = false;
QString fileName = QFileDialog::getOpenFileName(this, tr("Select a nonogram package to open"),
QString(), tr("QCross Package (*.cpk)"));
if (!fileName.isEmpty()) {
if (m_Package)
delete m_Package;
m_Package = CCrossPackage::read(fileName);
if (m_Package) {
statusBar()->showMessage(tr("Package \"%1\" opened.").arg(m_Package->name()));
}
qobject_cast<CCrossPackageModel *>(m_PicListView->model())->setPackage(m_Package);
}
}
bool CMainWindow::fileSave() {
if (m_Package->fileName().isEmpty())
return fileSaveAs();
bool result = m_Package->save();
if (result) {
statusBar()->showMessage(tr("Package \"%1\" saved.").arg(m_Package->name()));
m_Unsaved = false;
}
else
statusBar()->showMessage(tr("Error occured on saving.").arg(m_Package->name()));
return result;
}
bool CMainWindow::fileSaveAs() {
QString fileName = QFileDialog::getSaveFileName(this, tr("Select a file name for the current package"),
QString(), tr("QCross Package (*.cpk)"));
if (!fileName.isEmpty()) {
if (!fileName.endsWith(".cpk", Qt::CaseInsensitive))
fileName.append(".cpk");
m_Package->setFileName(fileName);
return fileSave();
}
return false;
}
void CMainWindow::editCreateEmpty() {
if (qobject_cast<CCrossPackageModel *>(m_PicListView->model())->appendEmpty(tr("untitled"), QSize(32, 32), false))
m_Unsaved = true;
}
void CMainWindow::editCreateFromPicture() {
QStringList fileNames = QFileDialog::getOpenFileNames(this, tr("Select a image file to import"),
QString(), tr("Images (*.png *.xpm *.xbm *.jpg)"));
CCrossPackageModel *model = qobject_cast<CCrossPackageModel *>(m_PicListView->model());
foreach (QString fileName, fileNames) {
if (!fileName.isEmpty()) {
QString title = QDir::toNativeSeparators(fileName).section(QDir::separator(), -1);
if (model->appendImage(title, QImage(fileName)))
m_Unsaved = true;
}
}
}
void CMainWindow::editRename() {
}
void CMainWindow::editSetTimeout() {
QInputDialog::getInt(this, tr("Timeout"), tr("Set time-out for selected nonogram (in minutes)"));
}
void CMainWindow::editDelete() {
QModelIndexList selectedIndexes = m_PicListView->selectionModel()->selectedIndexes();
foreach (QModelIndex i, selectedIndexes)
m_PicListView->model()->removeRow(i.row());
}
void CMainWindow::editSetPackageName() {
QString newName = QInputDialog::getText(this, tr("Set new package name"),
tr("Please enter a new name for this package:"), QLineEdit::Normal, m_Package->name());
if (!newName.isEmpty() && m_Package)
m_Package->setName(newName);
}
void CMainWindow::helpAbout() {
QMessageBox::about(this, tr("About"), tr("This is still an unfancy gui for creating and editing nonogram packages."));
}
}