From 25b7cd33eab7c8d039bdfef1d706538b166b3861 Mon Sep 17 00:00:00 2001 From: Marc Zinnschlag Date: Sun, 8 Sep 2013 14:31:20 +0200 Subject: [PATCH] added FileWidget; fixed OpenCS configuration --- apps/opencs/CMakeLists.txt | 2 +- apps/opencs/editor.cpp | 54 +++++++++++++++++++---- apps/opencs/editor.hpp | 3 ++ apps/opencs/main.cpp | 4 +- apps/opencs/view/doc/filewidget.cpp | 54 +++++++++++++++++++++++ apps/opencs/view/doc/filewidget.hpp | 42 ++++++++++++++++++ apps/opencs/view/doc/newgame.cpp | 44 +++++++++++++++++- apps/opencs/view/doc/newgame.hpp | 21 ++++++++- components/files/configurationmanager.cpp | 14 +++++- components/files/configurationmanager.hpp | 4 +- 10 files changed, 225 insertions(+), 17 deletions(-) create mode 100644 apps/opencs/view/doc/filewidget.cpp create mode 100644 apps/opencs/view/doc/filewidget.hpp diff --git a/apps/opencs/CMakeLists.txt b/apps/opencs/CMakeLists.txt index 8b9e31e3a..916ae6e24 100644 --- a/apps/opencs/CMakeLists.txt +++ b/apps/opencs/CMakeLists.txt @@ -43,7 +43,7 @@ opencs_units_noqt (model/tools opencs_units (view/doc - viewmanager view operations operation subview startup filedialog newgame + viewmanager view operations operation subview startup filedialog newgame filewidget ) diff --git a/apps/opencs/editor.cpp b/apps/opencs/editor.cpp index afbe233e4..09e80d73b 100644 --- a/apps/opencs/editor.cpp +++ b/apps/opencs/editor.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include "model/doc/document.hpp" #include "model/world/data.hpp" @@ -25,6 +26,9 @@ CS::Editor::Editor() : mViewManager (mDocumentManager) connect (&mFileDialog, SIGNAL(openFiles()), this, SLOT(openFiles())); connect (&mFileDialog, SIGNAL(createNewFile()), this, SLOT(createNewFile())); + connect (&mNewGame, SIGNAL (createRequest (const QString&)), + this, SLOT (createNewGame (const QString&))); + setupDataFiles(); } @@ -43,26 +47,39 @@ void CS::Editor::setupDataFiles() mCfgMgr.readConfiguration(variables, desc); - Files::PathContainer mDataDirs, mDataLocal; + Files::PathContainer dataDirs, dataLocal; if (!variables["data"].empty()) { - mDataDirs = Files::PathContainer(variables["data"].as()); + dataDirs = Files::PathContainer(variables["data"].as()); } std::string local = variables["data-local"].as(); if (!local.empty()) { - mDataLocal.push_back(Files::PathContainer::value_type(local)); + dataLocal.push_back(Files::PathContainer::value_type(local)); } - mCfgMgr.processPaths(mDataDirs); - mCfgMgr.processPaths(mDataLocal); + mCfgMgr.processPaths (dataDirs); + mCfgMgr.processPaths (dataLocal, true); + + if (!dataLocal.empty()) + mLocal = dataLocal[0]; + else + { + QMessageBox messageBox; + messageBox.setWindowTitle (tr ("No local data path available")); + messageBox.setIcon (QMessageBox::Critical); + messageBox.setStandardButtons (QMessageBox::Ok); + messageBox.setText(tr("
OpenCS is unable to access the local data directory. This may indicate a faulty configuration or a broken install.")); + messageBox.exec(); + + QApplication::exit (1); + return; + } // Set the charset for reading the esm/esp files QString encoding = QString::fromStdString(variables["encoding"].as()); mFileDialog.setEncoding(encoding); - Files::PathContainer dataDirs; - dataDirs.insert(dataDirs.end(), mDataDirs.begin(), mDataDirs.end()); - dataDirs.insert(dataDirs.end(), mDataLocal.begin(), mDataLocal.end()); + dataDirs.insert (dataDirs.end(), dataLocal.begin(), dataLocal.end()); for (Files::PathContainer::const_iterator iter = dataDirs.begin(); iter != dataDirs.end(); ++iter) { @@ -73,7 +90,6 @@ void CS::Editor::setupDataFiles() //load the settings into the userSettings instance. const QString settingFileName = "opencs.cfg"; CSMSettings::UserSettings::instance().loadSettings(settingFileName); - } void CS::Editor::createGame() @@ -133,6 +149,23 @@ void CS::Editor::createNewFile() mFileDialog.hide(); } +void CS::Editor::createNewGame (const QString& file) +{ + boost::filesystem::path path (mLocal); + + path /= file.toUtf8().data(); + + std::vector files; + + files.push_back (path); + + CSMDoc::Document *document = mDocumentManager.addDocument (files, true); + + mViewManager.addView (document); + + mNewGame.hide(); +} + void CS::Editor::showStartup() { if(mStartup.isHidden()) @@ -173,6 +206,9 @@ void CS::Editor::connectToIPCServer() int CS::Editor::run() { + if (mLocal.empty()) + return 1; + mStartup.show(); QApplication::setQuitOnLastWindowClosed (true); diff --git a/apps/opencs/editor.hpp b/apps/opencs/editor.hpp index 7d86358b3..d9e1ca6bc 100644 --- a/apps/opencs/editor.hpp +++ b/apps/opencs/editor.hpp @@ -35,6 +35,8 @@ namespace CS FileDialog mFileDialog; Files::ConfigurationManager mCfgMgr; + boost::filesystem::path mLocal; + void setupDataFiles(); // not implemented @@ -59,6 +61,7 @@ namespace CS void loadDocument(); void openFiles(); void createNewFile(); + void createNewGame (const QString& file); void showStartup(); diff --git a/apps/opencs/main.cpp b/apps/opencs/main.cpp index eddeb1983..ef7123c20 100644 --- a/apps/opencs/main.cpp +++ b/apps/opencs/main.cpp @@ -41,8 +41,8 @@ int main(int argc, char *argv[]) if(!editor.makeIPCServer()) { - editor.connectToIPCServer(); - return 0; + editor.connectToIPCServer(); + return 0; } return editor.run(); diff --git a/apps/opencs/view/doc/filewidget.cpp b/apps/opencs/view/doc/filewidget.cpp new file mode 100644 index 000000000..bcd6b83f2 --- /dev/null +++ b/apps/opencs/view/doc/filewidget.cpp @@ -0,0 +1,54 @@ + +#include "filewidget.hpp" + +#include +#include +#include +#include +#include + +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 stateChanged (!text.isEmpty()); + emit nameChanged (getName()); +} \ No newline at end of file diff --git a/apps/opencs/view/doc/filewidget.hpp b/apps/opencs/view/doc/filewidget.hpp new file mode 100644 index 000000000..58d52db92 --- /dev/null +++ b/apps/opencs/view/doc/filewidget.hpp @@ -0,0 +1,42 @@ +#ifndef CSV_DOC_FILEWIDGET_H +#define CSV_DOC_FILEWIDGET_H + +#include + +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 stateChanged (bool valid); + + void nameChanged (const QString& file); + }; +} + +#endif diff --git a/apps/opencs/view/doc/newgame.cpp b/apps/opencs/view/doc/newgame.cpp index eea0823a7..7ff0f38dc 100644 --- a/apps/opencs/view/doc/newgame.cpp +++ b/apps/opencs/view/doc/newgame.cpp @@ -3,12 +3,54 @@ #include #include +#include +#include +#include + +#include "filewidget.hpp" CSVDoc::NewGameDialogue::NewGameDialogue() { setWindowTitle ("Create New Game"); + QVBoxLayout *layout = new QVBoxLayout (this); + + mFileWidget = new FileWidget (this); + mFileWidget->setType (false); + + layout->addWidget (mFileWidget, 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 (mFileWidget, SIGNAL (stateChanged (bool)), this, SLOT (stateChanged (bool))); + connect (mCreate, SIGNAL (clicked()), this, SLOT (create())); + connect (cancel, SIGNAL (clicked()), this, SLOT (reject())); + QRect scr = QApplication::desktop()->screenGeometry(); QRect rect = geometry(); move (scr.center().x() - rect.center().x(), scr.center().y() - rect.center().y()); -} \ No newline at end of file +} + +void CSVDoc::NewGameDialogue::stateChanged (bool valid) +{ + mCreate->setEnabled (valid); +} + +void CSVDoc::NewGameDialogue::create() +{ + emit createRequest (mFileWidget->getName()); +} diff --git a/apps/opencs/view/doc/newgame.hpp b/apps/opencs/view/doc/newgame.hpp index 7c9e92a76..5e581e417 100644 --- a/apps/opencs/view/doc/newgame.hpp +++ b/apps/opencs/view/doc/newgame.hpp @@ -1,17 +1,34 @@ #ifndef CSV_DOC_NEWGAME_H #define CSV_DOC_NEWGAME_H -#include +#include + +class QPushButton; namespace CSVDoc { - class NewGameDialogue : public QWidget + class FileWidget; + + class NewGameDialogue : public QDialog { Q_OBJECT + QPushButton *mCreate; + FileWidget *mFileWidget; + public: NewGameDialogue(); + + signals: + + void createRequest (const QString& file); + + private slots: + + void stateChanged (bool valid); + + void create(); }; } diff --git a/components/files/configurationmanager.cpp b/components/files/configurationmanager.cpp index 56e55a98d..75c877dc5 100644 --- a/components/files/configurationmanager.cpp +++ b/components/files/configurationmanager.cpp @@ -56,7 +56,7 @@ void ConfigurationManager::readConfiguration(boost::program_options::variables_m } -void ConfigurationManager::processPaths(Files::PathContainer& dataDirs) +void ConfigurationManager::processPaths(Files::PathContainer& dataDirs, bool create) { std::string path; for (Files::PathContainer::iterator it = dataDirs.begin(); it != dataDirs.end(); ++it) @@ -94,6 +94,18 @@ void ConfigurationManager::processPaths(Files::PathContainer& dataDirs) if (!boost::filesystem::is_directory(*it)) { + if (create) + { + try + { + boost::filesystem::create_directories (*it); + } + catch (...) {} + + if (boost::filesystem::is_directory(*it)) + continue; + } + (*it).clear(); } } diff --git a/components/files/configurationmanager.hpp b/components/files/configurationmanager.hpp index 765f1cebf..4df871664 100644 --- a/components/files/configurationmanager.hpp +++ b/components/files/configurationmanager.hpp @@ -30,7 +30,9 @@ struct ConfigurationManager void readConfiguration(boost::program_options::variables_map& variables, boost::program_options::options_description& description); - void processPaths(Files::PathContainer& dataDirs); + + void processPaths(Files::PathContainer& dataDirs, bool create = false); + ///< \param create Try creating the directory, if it does not exist. /**< Fixed paths */ const boost::filesystem::path& getGlobalPath() const;