-
-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implementation of custom element assignment from file
Signed-off-by: Adarsh Balasubramanian <[email protected]>
- Loading branch information
Showing
5 changed files
with
346 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
avogadro/qtplugins/customelements/backgroundfileformat.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/****************************************************************************** | ||
This source file is part of the Avogadro project. | ||
Copyright 2013 Kitware, Inc. | ||
This source code is released under the New BSD License, (the "License"). | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
******************************************************************************/ | ||
|
||
#include "backgroundfileformat.h" | ||
|
||
#include <avogadro/io/fileformat.h> | ||
|
||
namespace Avogadro { | ||
|
||
BackgroundFileFormat::BackgroundFileFormat(Io::FileFormat* format, | ||
QObject* aparent) | ||
: QObject(aparent) | ||
, m_format(format) | ||
, m_molecule(nullptr) | ||
, m_success(false) | ||
{} | ||
|
||
BackgroundFileFormat::~BackgroundFileFormat() | ||
{ | ||
delete m_format; | ||
} | ||
|
||
void BackgroundFileFormat::read() | ||
{ | ||
m_success = false; | ||
m_error.clear(); | ||
|
||
if (!m_molecule) | ||
m_error = tr("No molecule set in BackgroundFileFormat!"); | ||
|
||
if (!m_format) | ||
m_error = tr("No Io::FileFormat set in BackgroundFileFormat!"); | ||
|
||
if (m_fileName.isEmpty()) | ||
m_error = tr("No file name set in BackgroundFileFormat!"); | ||
|
||
if (m_error.isEmpty()) { | ||
m_success = | ||
m_format->readFile(m_fileName.toLocal8Bit().data(), *m_molecule); | ||
|
||
if (!m_success) | ||
m_error = QString::fromStdString(m_format->error()); | ||
} | ||
|
||
emit finished(); | ||
} | ||
|
||
void BackgroundFileFormat::write() | ||
{ | ||
m_success = false; | ||
m_error.clear(); | ||
|
||
if (!m_molecule) | ||
m_error = tr("No molecule set in BackgroundFileFormat!"); | ||
|
||
if (!m_format) | ||
m_error = tr("No Io::FileFormat set in BackgroundFileFormat!"); | ||
|
||
if (m_fileName.isEmpty()) | ||
m_error = tr("No file name set in BackgroundFileFormat!"); | ||
|
||
if (m_error.isEmpty()) { | ||
m_success = | ||
m_format->writeFile(m_fileName.toLocal8Bit().data(), *m_molecule); | ||
|
||
if (!m_success) | ||
m_error = QString::fromStdString(m_format->error()); | ||
} | ||
|
||
emit finished(); | ||
} | ||
|
||
} // namespace Avogadro |
107 changes: 107 additions & 0 deletions
107
avogadro/qtplugins/customelements/backgroundfileformat.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/****************************************************************************** | ||
This source file is part of the Avogadro project. | ||
Copyright 2013 Kitware, Inc. | ||
This source code is released under the New BSD License, (the "License"). | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
******************************************************************************/ | ||
|
||
#ifndef AVOGADRO_BACKGROUNDFILEFORMAT_H | ||
#define AVOGADRO_BACKGROUNDFILEFORMAT_H | ||
|
||
#include <QtCore/QObject> | ||
#include <QtCore/QString> | ||
|
||
namespace Avogadro { | ||
|
||
namespace Core { | ||
class Molecule; | ||
} | ||
|
||
namespace Io { | ||
class FileFormat; | ||
} | ||
|
||
/** | ||
* @brief The BackgroundFileFormat class provides a thin QObject wrapper around | ||
* an instance of Io::FileFormat. | ||
*/ | ||
class BackgroundFileFormat : public QObject | ||
{ | ||
Q_OBJECT | ||
public: | ||
/** | ||
* This class takes ownership of @a format and will delete it when destructed. | ||
*/ | ||
explicit BackgroundFileFormat(Io::FileFormat* format, QObject* aparent = 0); | ||
~BackgroundFileFormat(); | ||
|
||
/** | ||
* The molecule instance to read/write. | ||
* @{ | ||
*/ | ||
void setMolecule(Core::Molecule* mol) { m_molecule = mol; } | ||
Core::Molecule* molecule() const { return m_molecule; } | ||
/**@}*/ | ||
|
||
/** | ||
* The name of the file to read/write. | ||
* @{ | ||
*/ | ||
void setFileName(const QString& filename) { m_fileName = filename; } | ||
QString fileName() const { return m_fileName; } | ||
/**@}*/ | ||
|
||
/** | ||
* The Io::FileFormat to use. | ||
*/ | ||
Io::FileFormat* fileFormat() const { return m_format; } | ||
|
||
/** | ||
* @return True if the operation was successful. | ||
*/ | ||
bool success() const { return m_success; } | ||
|
||
/** | ||
* @return An error string, set if success() is false. | ||
*/ | ||
QString error() const { return m_error; } | ||
|
||
signals: | ||
|
||
/** | ||
* Emitted when a call to read or write is called. | ||
*/ | ||
void finished(); | ||
|
||
public slots: | ||
|
||
/** | ||
* Use the fileFormat() to read fileName() into molecule(). | ||
*/ | ||
void read(); | ||
|
||
/** | ||
* Use the fileFormat() to write fileName() from molecule(). | ||
*/ | ||
void write(); | ||
|
||
private: | ||
Io::FileFormat* m_format; | ||
Core::Molecule* m_molecule; | ||
QString m_fileName; | ||
QString m_error; | ||
bool m_success; | ||
}; | ||
|
||
} // namespace Avogadro | ||
|
||
#endif // AVOGADRO_BACKGROUNDFILEFORMAT_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.