2022-08-19 15:59:36 +02:00
|
|
|
// Copyright (C) 2016 Nicolas Arnaud-Cormos
|
2022-12-21 10:12:09 +01:00
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
2010-12-20 10:35:30 +01:00
|
|
|
|
|
|
|
|
#include "macro.h"
|
2011-01-24 12:29:48 +01:00
|
|
|
#include "macroevent.h"
|
2010-12-20 10:35:30 +01:00
|
|
|
|
2011-08-30 15:57:00 +02:00
|
|
|
#include <app/app_version.h>
|
2011-03-30 15:15:15 +02:00
|
|
|
#include <utils/fileutils.h>
|
|
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QFileInfo>
|
|
|
|
|
#include <QDataStream>
|
2010-12-20 10:35:30 +01:00
|
|
|
|
2013-08-23 14:33:43 +02:00
|
|
|
using namespace Macros::Internal;
|
2010-12-20 10:35:30 +01:00
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
\class Macros::Macro
|
|
|
|
|
|
2013-06-05 14:29:24 +02:00
|
|
|
\brief The Macro class represents a macro, which is more or less a list of
|
|
|
|
|
Macros::MacroEvent.
|
2010-12-20 10:35:30 +01:00
|
|
|
|
2013-09-06 17:23:10 +02:00
|
|
|
A macro is a list of events that can be replayed in \QC. A macro has
|
|
|
|
|
an header consisting of the \QC version where the macro was created
|
2010-12-20 10:35:30 +01:00
|
|
|
and a description.
|
|
|
|
|
The name of the macro is the filename without the extension.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
class Macro::MacroPrivate
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
MacroPrivate();
|
|
|
|
|
|
|
|
|
|
QString description;
|
|
|
|
|
QString version;
|
|
|
|
|
QString fileName;
|
|
|
|
|
QList<MacroEvent> events;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Macro::MacroPrivate::MacroPrivate() :
|
2012-11-26 20:55:11 +02:00
|
|
|
version(QLatin1String(Core::Constants::IDE_VERSION_LONG))
|
2010-12-20 10:35:30 +01:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ---------- Macro ------------
|
|
|
|
|
|
|
|
|
|
Macro::Macro() :
|
|
|
|
|
d(new MacroPrivate)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Macro::Macro(const Macro &other):
|
|
|
|
|
d(new MacroPrivate)
|
|
|
|
|
{
|
|
|
|
|
d->description = other.d->description;
|
|
|
|
|
d->version = other.d->version;
|
|
|
|
|
d->fileName = other.d->fileName;
|
|
|
|
|
d->events = other.d->events;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Macro::~Macro()
|
|
|
|
|
{
|
|
|
|
|
delete d;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Macro& Macro::operator=(const Macro &other)
|
|
|
|
|
{
|
|
|
|
|
if (this == &other)
|
|
|
|
|
return *this;
|
|
|
|
|
d->description = other.d->description;
|
|
|
|
|
d->version = other.d->version;
|
|
|
|
|
d->fileName = other.d->fileName;
|
|
|
|
|
d->events = other.d->events;
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-30 15:15:15 +02:00
|
|
|
bool Macro::load(QString fileName)
|
2010-12-20 10:35:30 +01:00
|
|
|
{
|
2020-01-15 19:10:34 +01:00
|
|
|
if (!d->events.isEmpty())
|
2011-03-30 15:15:15 +02:00
|
|
|
return true; // the macro is not empty
|
2010-12-20 10:35:30 +01:00
|
|
|
|
|
|
|
|
// Take the current filename if the parameter is null
|
|
|
|
|
if (fileName.isNull())
|
|
|
|
|
fileName = d->fileName;
|
|
|
|
|
else
|
|
|
|
|
d->fileName = fileName;
|
|
|
|
|
|
|
|
|
|
// Load all the macroevents
|
|
|
|
|
QFile file(fileName);
|
|
|
|
|
if (file.open(QFile::ReadOnly)) {
|
|
|
|
|
QDataStream stream(&file);
|
|
|
|
|
stream >> d->version;
|
|
|
|
|
stream >> d->description;
|
|
|
|
|
while (!stream.atEnd()) {
|
|
|
|
|
MacroEvent macroEvent;
|
|
|
|
|
macroEvent.load(stream);
|
|
|
|
|
append(macroEvent);
|
|
|
|
|
}
|
2011-03-30 15:15:15 +02:00
|
|
|
return true;
|
2010-12-20 10:35:30 +01:00
|
|
|
}
|
2011-03-30 15:15:15 +02:00
|
|
|
return false;
|
2010-12-20 10:35:30 +01:00
|
|
|
}
|
|
|
|
|
|
2011-03-30 15:15:15 +02:00
|
|
|
bool Macro::loadHeader(const QString &fileName)
|
2010-12-20 10:35:30 +01:00
|
|
|
{
|
|
|
|
|
d->fileName = fileName;
|
|
|
|
|
QFile file(fileName);
|
|
|
|
|
if (file.open(QFile::ReadOnly)) {
|
|
|
|
|
QDataStream stream(&file);
|
|
|
|
|
stream >> d->version;
|
|
|
|
|
stream >> d->description;
|
2011-03-30 15:15:15 +02:00
|
|
|
return true;
|
2010-12-20 10:35:30 +01:00
|
|
|
}
|
2011-03-30 15:15:15 +02:00
|
|
|
return false;
|
2010-12-20 10:35:30 +01:00
|
|
|
}
|
|
|
|
|
|
2011-03-30 15:15:15 +02:00
|
|
|
bool Macro::save(const QString &fileName, QWidget *parent)
|
2010-12-20 10:35:30 +01:00
|
|
|
{
|
2021-05-18 09:47:07 +02:00
|
|
|
Utils::FileSaver saver(Utils::FilePath::fromString(fileName));
|
2011-03-30 15:15:15 +02:00
|
|
|
if (!saver.hasError()) {
|
|
|
|
|
QDataStream stream(saver.file());
|
2010-12-20 10:35:30 +01:00
|
|
|
stream << d->version;
|
|
|
|
|
stream << d->description;
|
2022-10-07 14:46:06 +02:00
|
|
|
for (const MacroEvent &event : std::as_const(d->events)) {
|
2010-12-20 10:35:30 +01:00
|
|
|
event.save(stream);
|
|
|
|
|
}
|
2011-03-30 15:15:15 +02:00
|
|
|
saver.setResult(&stream);
|
2010-12-20 10:35:30 +01:00
|
|
|
}
|
2011-03-30 15:15:15 +02:00
|
|
|
if (!saver.finalize(parent))
|
|
|
|
|
return false;
|
|
|
|
|
d->fileName = fileName;
|
|
|
|
|
return true;
|
2010-12-20 10:35:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString Macro::displayName() const
|
|
|
|
|
{
|
|
|
|
|
QFileInfo fileInfo(d->fileName);
|
|
|
|
|
return fileInfo.baseName();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Macro::append(const MacroEvent &event)
|
|
|
|
|
{
|
|
|
|
|
d->events.append(event);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const QString &Macro::description() const
|
|
|
|
|
{
|
|
|
|
|
return d->description;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Macro::setDescription(const QString &text)
|
|
|
|
|
{
|
|
|
|
|
d->description = text;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const QString &Macro::version() const
|
|
|
|
|
{
|
|
|
|
|
return d->version;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const QString &Macro::fileName() const
|
|
|
|
|
{
|
|
|
|
|
return d->fileName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const QList<MacroEvent> &Macro::events() const
|
|
|
|
|
{
|
|
|
|
|
return d->events;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Macro::isWritable() const
|
|
|
|
|
{
|
|
|
|
|
QFileInfo fileInfo(d->fileName);
|
|
|
|
|
return fileInfo.exists() && fileInfo.isWritable();
|
|
|
|
|
}
|