added FileWidget; fixed OpenCS configuration

actorid
Marc Zinnschlag 11 years ago
parent ecedb60169
commit 25b7cd33ea

@ -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
)

@ -4,6 +4,7 @@
#include <QApplication>
#include <QLocalServer>
#include <QLocalSocket>
#include <QMessageBox>
#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<Files::PathContainer>());
dataDirs = Files::PathContainer(variables["data"].as<Files::PathContainer>());
}
std::string local = variables["data-local"].as<std::string>();
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("<br><b>OpenCS is unable to access the local data directory. This may indicate a faulty configuration or a broken install.</b>"));
messageBox.exec();
QApplication::exit (1);
return;
}
// Set the charset for reading the esm/esp files
QString encoding = QString::fromStdString(variables["encoding"].as<std::string>());
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<boost::filesystem::path> 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);

@ -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();

@ -41,8 +41,8 @@ int main(int argc, char *argv[])
if(!editor.makeIPCServer())
{
editor.connectToIPCServer();
return 0;
editor.connectToIPCServer();
return 0;
}
return editor.run();

@ -0,0 +1,54 @@
#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 stateChanged (!text.isEmpty());
emit nameChanged (getName());
}

@ -0,0 +1,42 @@
#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 stateChanged (bool valid);
void nameChanged (const QString& file);
};
}
#endif

@ -3,12 +3,54 @@
#include <QApplication>
#include <QDesktopWidget>
#include <QVBoxLayout>
#include <QDialogButtonBox>
#include <QPushButton>
#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());
}
}
void CSVDoc::NewGameDialogue::stateChanged (bool valid)
{
mCreate->setEnabled (valid);
}
void CSVDoc::NewGameDialogue::create()
{
emit createRequest (mFileWidget->getName());
}

@ -1,17 +1,34 @@
#ifndef CSV_DOC_NEWGAME_H
#define CSV_DOC_NEWGAME_H
#include <QWidget>
#include <QDialog>
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();
};
}

@ -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();
}
}

@ -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;

Loading…
Cancel
Save