added AdjusterWidget

This commit is contained in:
Marc Zinnschlag 2013-09-10 16:45:01 +02:00
parent 25b7cd33ea
commit e9f14449eb
14 changed files with 190 additions and 40 deletions

View file

@ -44,6 +44,7 @@ opencs_units_noqt (model/tools
opencs_units (view/doc opencs_units (view/doc
viewmanager view operations operation subview startup filedialog newgame filewidget viewmanager view operations operation subview startup filedialog newgame filewidget
adjusterwidget
) )

View file

@ -9,10 +9,15 @@
#include "model/doc/document.hpp" #include "model/doc/document.hpp"
#include "model/world/data.hpp" #include "model/world/data.hpp"
CS::Editor::Editor() : mViewManager (mDocumentManager) CS::Editor::Editor() : mViewManager (mDocumentManager)
{ {
mIpcServerName = "org.openmw.OpenCS"; mIpcServerName = "org.openmw.OpenCS";
setupDataFiles();
mNewGame.setLocalData (mLocal);
connect (&mViewManager, SIGNAL (newGameRequest ()), this, SLOT (createGame ())); connect (&mViewManager, SIGNAL (newGameRequest ()), this, SLOT (createGame ()));
connect (&mViewManager, SIGNAL (newAddonRequest ()), this, SLOT (createAddon ())); connect (&mViewManager, SIGNAL (newAddonRequest ()), this, SLOT (createAddon ()));
connect (&mViewManager, SIGNAL (loadDocumentRequest ()), this, SLOT (loadDocument ())); connect (&mViewManager, SIGNAL (loadDocumentRequest ()), this, SLOT (loadDocument ()));
@ -26,10 +31,8 @@ CS::Editor::Editor() : mViewManager (mDocumentManager)
connect (&mFileDialog, SIGNAL(openFiles()), this, SLOT(openFiles())); connect (&mFileDialog, SIGNAL(openFiles()), this, SLOT(openFiles()));
connect (&mFileDialog, SIGNAL(createNewFile()), this, SLOT(createNewFile())); connect (&mFileDialog, SIGNAL(createNewFile()), this, SLOT(createNewFile()));
connect (&mNewGame, SIGNAL (createRequest (const QString&)), connect (&mNewGame, SIGNAL (createRequest (const boost::filesystem::path&)),
this, SLOT (createNewGame (const QString&))); this, SLOT (createNewGame (const boost::filesystem::path&)));
setupDataFiles();
} }
void CS::Editor::setupDataFiles() void CS::Editor::setupDataFiles()
@ -126,7 +129,9 @@ void CS::Editor::openFiles()
files.push_back(path.toStdString()); files.push_back(path.toStdString());
} }
CSMDoc::Document *document = mDocumentManager.addDocument(files, false); /// \todo Get the save path from the file dialogue
CSMDoc::Document *document = mDocumentManager.addDocument (files, *files.rbegin(), false);
mViewManager.addView (document); mViewManager.addView (document);
mFileDialog.hide(); mFileDialog.hide();
@ -143,23 +148,21 @@ void CS::Editor::createNewFile()
files.push_back(mFileDialog.fileName().toStdString()); files.push_back(mFileDialog.fileName().toStdString());
CSMDoc::Document *document = mDocumentManager.addDocument (files, true); /// \todo Get the save path from the file dialogue.
CSMDoc::Document *document = mDocumentManager.addDocument (files, *files.rbegin(), true);
mViewManager.addView (document); mViewManager.addView (document);
mFileDialog.hide(); mFileDialog.hide();
} }
void CS::Editor::createNewGame (const QString& file) void CS::Editor::createNewGame (const boost::filesystem::path& file)
{ {
boost::filesystem::path path (mLocal);
path /= file.toUtf8().data();
std::vector<boost::filesystem::path> files; std::vector<boost::filesystem::path> files;
files.push_back (path); files.push_back (file);
CSMDoc::Document *document = mDocumentManager.addDocument (files, true); CSMDoc::Document *document = mDocumentManager.addDocument (files, file, true);
mViewManager.addView (document); mViewManager.addView (document);

View file

@ -61,7 +61,7 @@ namespace CS
void loadDocument(); void loadDocument();
void openFiles(); void openFiles();
void createNewFile(); void createNewFile();
void createNewGame (const QString& file); void createNewGame (const boost::filesystem::path& file);
void showStartup(); void showStartup();

View file

@ -2139,18 +2139,13 @@ void CSMDoc::Document::createBase()
} }
} }
CSMDoc::Document::Document (const std::vector<boost::filesystem::path>& files, bool new_) CSMDoc::Document::Document (const std::vector<boost::filesystem::path>& files,
: mTools (mData) const boost::filesystem::path& savePath, bool new_)
: mSavePath (savePath), mTools (mData)
{ {
if (files.empty()) if (files.empty())
throw std::runtime_error ("Empty content file sequence"); throw std::runtime_error ("Empty content file sequence");
/// \todo adjust last file name:
/// \li make sure it is located in the data-local directory (adjust path if necessary)
/// \li make sure the extension matches the new scheme (change it if necesarry)
mName = files.back().filename().string();
if (new_ && files.size()==1) if (new_ && files.size()==1)
createBase(); createBase();
else else
@ -2201,9 +2196,9 @@ int CSMDoc::Document::getState() const
return state; return state;
} }
const std::string& CSMDoc::Document::getName() const const boost::filesystem::path& CSMDoc::Document::getSavePath() const
{ {
return mName; return mSavePath;
} }
void CSMDoc::Document::save() void CSMDoc::Document::save()

View file

@ -31,7 +31,7 @@ namespace CSMDoc
private: private:
std::string mName; ///< \todo replace name with ESX list boost::filesystem::path mSavePath;
CSMWorld::Data mData; CSMWorld::Data mData;
CSMTools::Tools mTools; CSMTools::Tools mTools;
@ -64,15 +64,16 @@ namespace CSMDoc
public: public:
Document (const std::vector<boost::filesystem::path>& files, bool new_); Document (const std::vector<boost::filesystem::path>& files,
const boost::filesystem::path& savePath, bool new_);
~Document(); ~Document();
QUndoStack& getUndoStack(); QUndoStack& getUndoStack();
int getState() const; int getState() const;
const std::string& getName() const; const boost::filesystem::path& getSavePath() const;
///< \todo replace with ESX list
void save(); void save();

View file

@ -14,10 +14,10 @@ CSMDoc::DocumentManager::~DocumentManager()
delete *iter; delete *iter;
} }
CSMDoc::Document *CSMDoc::DocumentManager::addDocument (const std::vector<boost::filesystem::path>& files, CSMDoc::Document *CSMDoc::DocumentManager::addDocument (const std::vector<boost::filesystem::path>& files, const boost::filesystem::path& savePath,
bool new_) bool new_)
{ {
Document *document = new Document (files, new_); Document *document = new Document (files, savePath, new_);
mDocuments.push_back (document); mDocuments.push_back (document);

View file

@ -23,7 +23,8 @@ namespace CSMDoc
~DocumentManager(); ~DocumentManager();
Document *addDocument (const std::vector<boost::filesystem::path>& files, bool new_); Document *addDocument (const std::vector<boost::filesystem::path>& files,
const boost::filesystem::path& savePath, bool new_);
///< The ownership of the returned document is not transferred to the caller. ///< The ownership of the returned document is not transferred to the caller.
/// ///
/// \param new_ Do not load the last content file in \a files and instead create in an /// \param new_ Do not load the last content file in \a files and instead create in an

View file

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

View file

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

View file

@ -49,6 +49,5 @@ QString CSVDoc::FileWidget::getName() const
void CSVDoc::FileWidget::textChanged (const QString& text) void CSVDoc::FileWidget::textChanged (const QString& text)
{ {
emit stateChanged (!text.isEmpty()); emit nameChanged (getName(), mAddon);
emit nameChanged (getName());
} }

View file

@ -33,9 +33,7 @@ namespace CSVDoc
signals: signals:
void stateChanged (bool valid); void nameChanged (const QString& file, bool addon);
void nameChanged (const QString& file);
}; };
} }

View file

@ -8,6 +8,7 @@
#include <QPushButton> #include <QPushButton>
#include "filewidget.hpp" #include "filewidget.hpp"
#include "adjusterwidget.hpp"
CSVDoc::NewGameDialogue::NewGameDialogue() CSVDoc::NewGameDialogue::NewGameDialogue()
{ {
@ -20,6 +21,10 @@ CSVDoc::NewGameDialogue::NewGameDialogue()
layout->addWidget (mFileWidget, 1); layout->addWidget (mFileWidget, 1);
mAdjusterWidget = new AdjusterWidget (this);
layout->addWidget (mAdjusterWidget, 1);
QDialogButtonBox *buttons = new QDialogButtonBox (this); QDialogButtonBox *buttons = new QDialogButtonBox (this);
mCreate = new QPushButton ("Create", this); mCreate = new QPushButton ("Create", this);
@ -36,15 +41,22 @@ CSVDoc::NewGameDialogue::NewGameDialogue()
setLayout (layout); setLayout (layout);
connect (mFileWidget, SIGNAL (stateChanged (bool)), this, SLOT (stateChanged (bool))); connect (mAdjusterWidget, SIGNAL (stateChanged (bool)), this, SLOT (stateChanged (bool)));
connect (mCreate, SIGNAL (clicked()), this, SLOT (create())); connect (mCreate, SIGNAL (clicked()), this, SLOT (create()));
connect (cancel, SIGNAL (clicked()), this, SLOT (reject())); 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 scr = QApplication::desktop()->screenGeometry();
QRect rect = geometry(); QRect rect = geometry();
move (scr.center().x() - rect.center().x(), scr.center().y() - rect.center().y()); 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) void CSVDoc::NewGameDialogue::stateChanged (bool valid)
{ {
mCreate->setEnabled (valid); mCreate->setEnabled (valid);
@ -52,5 +64,5 @@ void CSVDoc::NewGameDialogue::stateChanged (bool valid)
void CSVDoc::NewGameDialogue::create() void CSVDoc::NewGameDialogue::create()
{ {
emit createRequest (mFileWidget->getName()); emit createRequest (mAdjusterWidget->getPath());
} }

View file

@ -1,13 +1,19 @@
#ifndef CSV_DOC_NEWGAME_H #ifndef CSV_DOC_NEWGAME_H
#define CSV_DOC_NEWGAME_H #define CSV_DOC_NEWGAME_H
#include <boost/filesystem/path.hpp>
#include <QDialog> #include <QDialog>
#include <QMetaType>
Q_DECLARE_METATYPE (boost::filesystem::path)
class QPushButton; class QPushButton;
namespace CSVDoc namespace CSVDoc
{ {
class FileWidget; class FileWidget;
class AdjusterWidget;
class NewGameDialogue : public QDialog class NewGameDialogue : public QDialog
{ {
@ -15,14 +21,17 @@ namespace CSVDoc
QPushButton *mCreate; QPushButton *mCreate;
FileWidget *mFileWidget; FileWidget *mFileWidget;
AdjusterWidget *mAdjusterWidget;
public: public:
NewGameDialogue(); NewGameDialogue();
void setLocalData (const boost::filesystem::path& localData);
signals: signals:
void createRequest (const QString& file); void createRequest (const boost::filesystem::path& file);
private slots: private slots:

View file

@ -184,7 +184,7 @@ void CSVDoc::View::updateTitle()
{ {
std::ostringstream stream; std::ostringstream stream;
stream << mDocument->getName(); stream << mDocument->getSavePath().filename().string();
if (mDocument->getState() & CSMDoc::State_Modified) if (mDocument->getState() & CSMDoc::State_Modified)
stream << " *"; stream << " *";