Merge branch 'newgame'
commit
554c44892a
@ -0,0 +1,90 @@
|
||||
|
||||
#include "adjusterwidget.hpp"
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
#include <boost/filesystem.hpp>
|
||||
|
||||
#include <QHBoxLayout>
|
||||
#include <QLabel>
|
||||
#include <QStyle>
|
||||
|
||||
CSVDoc::AdjusterWidget::AdjusterWidget (QWidget *parent)
|
||||
: QWidget (parent), mValid (false)
|
||||
{
|
||||
QHBoxLayout *layout = new QHBoxLayout (this);
|
||||
|
||||
mIcon = new QLabel (this);
|
||||
|
||||
layout->addWidget (mIcon, 0);
|
||||
|
||||
mMessage = new QLabel (this);
|
||||
mMessage->setWordWrap (true);
|
||||
mMessage->setSizePolicy (QSizePolicy (QSizePolicy::Minimum, QSizePolicy::Minimum));
|
||||
|
||||
layout->addWidget (mMessage, 1);
|
||||
|
||||
setName ("", false);
|
||||
|
||||
setLayout (layout);
|
||||
}
|
||||
|
||||
void CSVDoc::AdjusterWidget::setLocalData (const boost::filesystem::path& localData)
|
||||
{
|
||||
mLocalData = localData;
|
||||
}
|
||||
|
||||
boost::filesystem::path CSVDoc::AdjusterWidget::getPath() const
|
||||
{
|
||||
if (!mValid)
|
||||
throw std::logic_error ("invalid content file path");
|
||||
|
||||
return mResultPath;
|
||||
}
|
||||
|
||||
void CSVDoc::AdjusterWidget::setName (const QString& name, bool addon)
|
||||
{
|
||||
QString message;
|
||||
|
||||
if (name.isEmpty())
|
||||
{
|
||||
mValid = false;
|
||||
message = "No name.";
|
||||
}
|
||||
else
|
||||
{
|
||||
boost::filesystem::path path (name.toUtf8().data());
|
||||
|
||||
path.replace_extension (addon ? ".omwaddon" : ".omwgame");
|
||||
|
||||
if (path.parent_path().string()==mLocalData.string())
|
||||
{
|
||||
// path already points to the local data directory
|
||||
message = QString::fromUtf8 (("Will be saved as: " + path.native()).c_str());
|
||||
mResultPath = path;
|
||||
mValid = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
// path points somewhere else or is a leaf name.
|
||||
path = mLocalData / path.filename();
|
||||
|
||||
message = QString::fromUtf8 (("Will be saved as: " + path.native()).c_str());
|
||||
mResultPath = path;
|
||||
mValid = true;
|
||||
|
||||
if (boost::filesystem::exists (path))
|
||||
{
|
||||
/// \todo add an user setting to make this an error.
|
||||
message += "<p>But a file with the same name already exists. If you continue, it will be overwritten.";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
mMessage->setText (message);
|
||||
mIcon->setPixmap (style()->standardIcon (
|
||||
mValid ? QStyle::SP_MessageBoxInformation : QStyle::SP_MessageBoxWarning).
|
||||
pixmap (QSize (16, 16)));
|
||||
|
||||
emit stateChanged (mValid);
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
#ifndef CSV_DOC_ADJUSTERWIDGET_H
|
||||
#define CSV_DOC_ADJUSTERWIDGET_H
|
||||
|
||||
#include <boost/filesystem/path.hpp>
|
||||
|
||||
#include <QWidget>
|
||||
|
||||
class QLabel;
|
||||
|
||||
namespace CSVDoc
|
||||
{
|
||||
class AdjusterWidget : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
boost::filesystem::path mLocalData;
|
||||
QLabel *mMessage;
|
||||
QLabel *mIcon;
|
||||
bool mValid;
|
||||
boost::filesystem::path mResultPath;
|
||||
|
||||
public:
|
||||
|
||||
AdjusterWidget (QWidget *parent = 0);
|
||||
|
||||
void setLocalData (const boost::filesystem::path& localData);
|
||||
|
||||
boost::filesystem::path getPath() const;
|
||||
///< This function must not be called if there is no valid path.
|
||||
|
||||
public slots:
|
||||
|
||||
void setName (const QString& name, bool addon);
|
||||
|
||||
signals:
|
||||
|
||||
void stateChanged (bool valid);
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
@ -0,0 +1,53 @@
|
||||
|
||||
#include "filewidget.hpp"
|
||||
|
||||
#include <QHBoxLayout>
|
||||
#include <QLineEdit>
|
||||
#include <QLabel>
|
||||
#include <QRegExpValidator>
|
||||
#include <QRegExp>
|
||||
|
||||
QString CSVDoc::FileWidget::getExtension() const
|
||||
{
|
||||
return mAddon ? ".omwaddon" : ".omwgame";
|
||||
}
|
||||
|
||||
CSVDoc::FileWidget::FileWidget (QWidget *parent) : QWidget (parent), mAddon (false)
|
||||
{
|
||||
QHBoxLayout *layout = new QHBoxLayout (this);
|
||||
|
||||
mInput = new QLineEdit (this);
|
||||
mInput->setValidator (new QRegExpValidator(QRegExp("^[a-zA-Z0-9\\s]*$")));
|
||||
|
||||
layout->addWidget (mInput, 1);
|
||||
|
||||
mType = new QLabel (this);
|
||||
|
||||
layout ->addWidget (mType);
|
||||
|
||||
connect (mInput, SIGNAL (textChanged (const QString&)), this, SLOT (textChanged (const QString&)));
|
||||
|
||||
setLayout (layout);
|
||||
}
|
||||
|
||||
void CSVDoc::FileWidget::setType (bool addon)
|
||||
{
|
||||
mAddon = addon;
|
||||
|
||||
mType->setText (getExtension());
|
||||
}
|
||||
|
||||
QString CSVDoc::FileWidget::getName() const
|
||||
{
|
||||
QString text = mInput->text();
|
||||
|
||||
if (text.isEmpty())
|
||||
return "";
|
||||
|
||||
return text + getExtension();
|
||||
}
|
||||
|
||||
void CSVDoc::FileWidget::textChanged (const QString& text)
|
||||
{
|
||||
emit nameChanged (getName(), mAddon);
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
#ifndef CSV_DOC_FILEWIDGET_H
|
||||
#define CSV_DOC_FILEWIDGET_H
|
||||
|
||||
#include <QWidget>
|
||||
|
||||
class QLabel;
|
||||
class QString;
|
||||
class QLineEdit;
|
||||
|
||||
namespace CSVDoc
|
||||
{
|
||||
class FileWidget : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
bool mAddon;
|
||||
QLineEdit *mInput;
|
||||
QLabel *mType;
|
||||
|
||||
QString getExtension() const;
|
||||
|
||||
public:
|
||||
|
||||
FileWidget (QWidget *parent = 0);
|
||||
|
||||
void setType (bool addon);
|
||||
|
||||
QString getName() const;
|
||||
|
||||
private slots:
|
||||
|
||||
void textChanged (const QString& text);
|
||||
|
||||
signals:
|
||||
|
||||
void nameChanged (const QString& file, bool addon);
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
@ -0,0 +1,68 @@
|
||||
|
||||
#include "newgame.hpp"
|
||||
|
||||
#include <QApplication>
|
||||
#include <QDesktopWidget>
|
||||
#include <QVBoxLayout>
|
||||
#include <QDialogButtonBox>
|
||||
#include <QPushButton>
|
||||
|
||||
#include "filewidget.hpp"
|
||||
#include "adjusterwidget.hpp"
|
||||
|
||||
CSVDoc::NewGameDialogue::NewGameDialogue()
|
||||
{
|
||||
setWindowTitle ("Create New Game");
|
||||
|
||||
QVBoxLayout *layout = new QVBoxLayout (this);
|
||||
|
||||
mFileWidget = new FileWidget (this);
|
||||
mFileWidget->setType (false);
|
||||
|
||||
layout->addWidget (mFileWidget, 1);
|
||||
|
||||
mAdjusterWidget = new AdjusterWidget (this);
|
||||
|
||||
layout->addWidget (mAdjusterWidget, 1);
|
||||
|
||||
QDialogButtonBox *buttons = new QDialogButtonBox (this);
|
||||
|
||||
mCreate = new QPushButton ("Create", this);
|
||||
mCreate->setDefault (true);
|
||||
mCreate->setEnabled (false);
|
||||
|
||||
buttons->addButton (mCreate, QDialogButtonBox::AcceptRole);
|
||||
|
||||
QPushButton *cancel = new QPushButton ("Cancel", this);
|
||||
|
||||
buttons->addButton (cancel, QDialogButtonBox::RejectRole);
|
||||
|
||||
layout->addWidget (buttons);
|
||||
|
||||
setLayout (layout);
|
||||
|
||||
connect (mAdjusterWidget, SIGNAL (stateChanged (bool)), this, SLOT (stateChanged (bool)));
|
||||
connect (mCreate, SIGNAL (clicked()), this, SLOT (create()));
|
||||
connect (cancel, SIGNAL (clicked()), this, SLOT (reject()));
|
||||
connect (mFileWidget, SIGNAL (nameChanged (const QString&, bool)),
|
||||
mAdjusterWidget, SLOT (setName (const QString&, bool)));
|
||||
|
||||
QRect scr = QApplication::desktop()->screenGeometry();
|
||||
QRect rect = geometry();
|
||||
move (scr.center().x() - rect.center().x(), scr.center().y() - rect.center().y());
|
||||
}
|
||||
|
||||
void CSVDoc::NewGameDialogue::setLocalData (const boost::filesystem::path& localData)
|
||||
{
|
||||
mAdjusterWidget->setLocalData (localData);
|
||||
}
|
||||
|
||||
void CSVDoc::NewGameDialogue::stateChanged (bool valid)
|
||||
{
|
||||
mCreate->setEnabled (valid);
|
||||
}
|
||||
|
||||
void CSVDoc::NewGameDialogue::create()
|
||||
{
|
||||
emit createRequest (mAdjusterWidget->getPath());
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
#ifndef CSV_DOC_NEWGAME_H
|
||||
#define CSV_DOC_NEWGAME_H
|
||||
|
||||
#include <boost/filesystem/path.hpp>
|
||||
|
||||
#include <QDialog>
|
||||
#include <QMetaType>
|
||||
|
||||
Q_DECLARE_METATYPE (boost::filesystem::path)
|
||||
|
||||
class QPushButton;
|
||||
|
||||
namespace CSVDoc
|
||||
{
|
||||
class FileWidget;
|
||||
class AdjusterWidget;
|
||||
|
||||
class NewGameDialogue : public QDialog
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
QPushButton *mCreate;
|
||||
FileWidget *mFileWidget;
|
||||
AdjusterWidget *mAdjusterWidget;
|
||||
|
||||
public:
|
||||
|
||||
NewGameDialogue();
|
||||
|
||||
void setLocalData (const boost::filesystem::path& localData);
|
||||
|
||||
signals:
|
||||
|
||||
void createRequest (const boost::filesystem::path& file);
|
||||
|
||||
private slots:
|
||||
|
||||
void stateChanged (bool valid);
|
||||
|
||||
void create();
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue