Members of the KDE Community are recommended to subscribe to the kde-community mailing list at https://mail.kde.org/mailman/listinfo/kde-community to allow them to participate in important discussions and receive other important announcements

Commit d4735725 authored by Gustavo Carneiro's avatar Gustavo Carneiro

Move ProfileGroup and ProfileCommandParser Classes to a new files.

parent 0beeff76
......@@ -40,6 +40,7 @@
#include "ViewManager.h"
#include "WindowSystemInfo.h"
#include "profile/ProfileManager.h"
#include "profile/ProfileCommandParser.h"
#include "session/Session.h"
#include "session/SessionManager.h"
#include "widgets/TerminalDisplay.h"
......
......@@ -26,6 +26,7 @@
// Konsole
#include "../profile/Profile.h"
#include "../profile/ProfileGroup.h"
#include "../profile/ProfileWriter.h"
#include <array>
......
......@@ -12,6 +12,8 @@ add_library(konsoleprofile
STATIC
EditProfileDialog.cpp
Profile.cpp
ProfileCommandParser.cpp
ProfileGroup.cpp
ProfileList.cpp
ProfileReader.cpp
ProfileWriter.cpp
......
......@@ -27,6 +27,7 @@
// Konsole
#include "Profile.h"
#include "ProfileGroup.h"
#include "Enumeration.h"
#include "ColorScheme.h"
#include "ColorSchemeEditor.h"
......
......@@ -32,6 +32,7 @@
// Konsole
#include "Enumeration.h"
#include "ProfileGroup.h"
using namespace Konsole;
......@@ -340,68 +341,14 @@ const QStringList Profile::propertiesInfoList() const
return info;
}
QHash<Profile::Property, QVariant> ProfileCommandParser::parse(const QString& input)
const Profile::GroupPtr Profile::asGroup() const
{
QHash<Profile::Property, QVariant> changes;
// regular expression to parse profile change requests.
//
// format: property=value;property=value ...
//
// where 'property' is a word consisting only of characters from A-Z
// where 'value' is any sequence of characters other than a semi-colon
//
static const QRegularExpression regExp(QStringLiteral("([a-zA-Z]+)=([^;]+)"));
QRegularExpressionMatchIterator iterator(regExp.globalMatch(input));
while (iterator.hasNext()) {
QRegularExpressionMatch match(iterator.next());
Profile::Property property = Profile::lookupByName(match.captured(1));
const QString value = match.captured(2);
changes.insert(property, value);
}
return changes;
}
void ProfileGroup::updateValues()
{
const PropertyInfo* properties = Profile::DefaultPropertyNames;
while (properties->name != nullptr) {
// the profile group does not store a value for some properties
// (eg. name, path) if even they are equal between profiles -
//
// the exception is when the group has only one profile in which
// case it behaves like a standard Profile
if (_profiles.count() > 1 &&
!canInheritProperty(properties->property)) {
properties++;
continue;
}
QVariant value;
for (int i = 0; i < _profiles.count(); i++) {
QVariant profileValue = _profiles[i]->property<QVariant>(properties->property);
if (value.isNull()) {
value = profileValue;
} else if (value != profileValue) {
value = QVariant();
break;
}
}
Profile::setProperty(properties->property, value);
properties++;
}
const Profile::GroupPtr ptr(dynamic_cast<ProfileGroup *>(
const_cast<Profile *>(this)));
return ptr;
}
void ProfileGroup::setProperty(Property p, const QVariant& value)
Profile::GroupPtr Profile::asGroup()
{
if (_profiles.count() > 1 && !canInheritProperty(p)) {
return;
}
Profile::setProperty(p, value);
for (const Profile::Ptr &profile : qAsConst(_profiles)) {
profile->setProperty(p, value);
}
return Profile::GroupPtr(dynamic_cast<ProfileGroup *>(this));
}
......@@ -714,119 +714,6 @@ inline QVariant Profile::property(Property p) const
}
}
/**
* A composite profile which allows a group of profiles to be treated as one.
* When setting a property, the new value is applied to all profiles in the
* group. When reading a property, if all profiles in the group have the same
* value then that value is returned, otherwise the result is null.
*
* Profiles can be added to the group using addProfile(). When all profiles
* have been added updateValues() must be called
* to sync the group's property values with those of the group's profiles.
*
* The Profile::Name and Profile::Path properties are unique to individual
* profiles, setting these properties on a ProfileGroup has no effect.
*/
class KONSOLEPRIVATE_EXPORT ProfileGroup : public Profile
{
public:
using Ptr = QExplicitlySharedDataPointer<ProfileGroup>;
/** Construct a new profile group, which is hidden by default. */
explicit ProfileGroup(const Profile::Ptr &profileParent = Profile::Ptr());
/** Add a profile to the group. Calling setProperty() will update this
* profile. When creating a group, add the profiles to the group then
* call updateValues() to make the group's property values reflect the
* profiles currently in the group.
*/
void addProfile(const Profile::Ptr &profile)
{
_profiles.append(profile);
}
/** Remove a profile from the group. Calling setProperty() will no longer
* affect this profile. */
void removeProfile(const Profile::Ptr &profile)
{
_profiles.removeAll(profile);
}
/** Returns the profiles in this group .*/
QList<Profile::Ptr> profiles() const
{
return _profiles;
}
/**
* Updates the property values in this ProfileGroup to match those from
* the group's profiles()
*
* For each available property, if each profile in the group has the same
* value then the ProfileGroup will use that value for the property.
* Otherwise the value for the property will be set to a null QVariant
*
* Some properties such as the name and the path of the profile
* will always be set to null if the group has more than one profile.
*/
void updateValues();
/** Sets the value of @p property in each of the group's profiles to
* @p value.
*/
void setProperty(Property p, const QVariant &value) override;
private:
Q_DISABLE_COPY(ProfileGroup)
QList<Profile::Ptr> _profiles;
};
inline ProfileGroup::ProfileGroup(const Profile::Ptr &profileParent) :
Profile(profileParent),
_profiles(QList<Profile::Ptr>())
{
setHidden(true);
}
inline const Profile::GroupPtr Profile::asGroup() const
{
const Profile::GroupPtr ptr(dynamic_cast<ProfileGroup *>(
const_cast<Profile *>(this)));
return ptr;
}
inline Profile::GroupPtr Profile::asGroup()
{
return Profile::GroupPtr(dynamic_cast<ProfileGroup *>(this));
}
/**
* Parses an input string consisting of property names
* and assigned values and returns a table of properties
* and values.
*
* The input string will typically look like this:
*
* @code
* PropertyName=Value;PropertyName=Value ...
* @endcode
*
* For example:
*
* @code
* Icon=konsole;Directory=/home/bob
* @endcode
*/
class KONSOLEPRIVATE_EXPORT ProfileCommandParser
{
public:
/**
* Parses an input string consisting of property names
* and assigned values and returns a table of
* properties and values.
*/
QHash<Profile::Property, QVariant> parse(const QString &input);
};
}
Q_DECLARE_METATYPE(Konsole::Profile::Ptr)
......
/*
This source file is part of Konsole, a terminal emulator.
Copyright 2007-2008 by Robert Knight <robertknight@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.
*/
// Own
#include "ProfileCommandParser.h"
// Konsole
#include "Profile.h"
// Qt
#include <QHash>
#include <QVariant>
#include <QRegularExpression>
using namespace Konsole;
QHash<Profile::Property, QVariant> ProfileCommandParser::parse(const QString& input)
{
QHash<Profile::Property, QVariant> changes;
// regular expression to parse profile change requests.
//
// format: property=value;property=value ...
//
// where 'property' is a word consisting only of characters from A-Z
// where 'value' is any sequence of characters other than a semi-colon
//
static const QRegularExpression regExp(QStringLiteral("([a-zA-Z]+)=([^;]+)"));
QRegularExpressionMatchIterator iterator(regExp.globalMatch(input));
while (iterator.hasNext()) {
QRegularExpressionMatch match(iterator.next());
Profile::Property property = Profile::lookupByName(match.captured(1));
const QString value = match.captured(2);
changes.insert(property, value);
}
return changes;
}
/*
This source file is part of Konsole, a terminal emulator.
Copyright 2007-2008 by Robert Knight <robertknight@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.
*/
#ifndef PROFILECOMMANDPARSER_H
#define PROFILECOMMANDPARSER_H
// Konsole
#include "konsoleprivate_export.h"
#include "Profile.h"
class QVariant;
template<typename, typename> class QHash;
namespace Konsole
{
/**
* Parses an input string consisting of property names
* and assigned values and returns a table of properties
* and values.
*
* The input string will typically look like this:
*
* @code
* PropertyName=Value;PropertyName=Value ...
* @endcode
*
* For example:
*
* @code
* Icon=konsole;Directory=/home/bob
* @endcode
*/
class KONSOLEPRIVATE_EXPORT ProfileCommandParser
{
public:
/**
* Parses an input string consisting of property names
* and assigned values and returns a table of
* properties and values.
*/
QHash<Profile::Property, QVariant> parse(const QString &input);
};
}
#endif
/*
This source file is part of Konsole, a terminal emulator.
Copyright 2007-2008 by Robert Knight <robertknight@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.
*/
// Own
#include "ProfileGroup.h"
// Konsole
#include "Profile.h"
using namespace Konsole;
void ProfileGroup::addProfile(const Profile::Ptr &profile)
{
_profiles.append(profile);
}
void ProfileGroup::removeProfile(const Profile::Ptr &profile)
{
_profiles.removeAll(profile);
}
QList<Profile::Ptr> ProfileGroup::profiles() const
{
return _profiles;
}
void ProfileGroup::updateValues()
{
const PropertyInfo* properties = Profile::DefaultPropertyNames;
while (properties->name != nullptr) {
// the profile group does not store a value for some properties
// (eg. name, path) if even they are equal between profiles -
//
// the exception is when the group has only one profile in which
// case it behaves like a standard Profile
if (_profiles.count() > 1 &&
!canInheritProperty(properties->property)) {
properties++;
continue;
}
QVariant value;
for (int i = 0; i < _profiles.count(); i++) {
QVariant profileValue = _profiles[i]->property<QVariant>(properties->property);
if (value.isNull()) {
value = profileValue;
} else if (value != profileValue) {
value = QVariant();
break;
}
}
Profile::setProperty(properties->property, value);
properties++;
}
}
void ProfileGroup::setProperty(Property p, const QVariant& value)
{
if (_profiles.count() > 1 && !canInheritProperty(p)) {
return;
}
Profile::setProperty(p, value);
for (const Profile::Ptr &profile : qAsConst(_profiles)) {
profile->setProperty(p, value);
}
}
/*
This source file is part of Konsole, a terminal emulator.
Copyright 2007-2008 by Robert Knight <robertknight@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.
*/
#ifndef PROFILEGROUP_H
#define PROFILEGROUP_H
// Konsole
#include "konsoleprivate_export.h"
#include "Profile.h"
// Qt
#include <QHash>
#include <QStringList>
#include <QVariant>
#include <QFont>
#include <QColor>
namespace Konsole
{
/**
* A composite profile which allows a group of profiles to be treated as one.
* When setting a property, the new value is applied to all profiles in the
* group. When reading a property, if all profiles in the group have the same
* value then that value is returned, otherwise the result is null.
*
* Profiles can be added to the group using addProfile(). When all profiles
* have been added updateValues() must be called
* to sync the group's property values with those of the group's profiles.
*
* The Profile::Name and Profile::Path properties are unique to individual
* profiles, setting these properties on a ProfileGroup has no effect.
*/
class KONSOLEPRIVATE_EXPORT ProfileGroup : public Profile
{
public:
using Ptr = QExplicitlySharedDataPointer<ProfileGroup>;
/** Construct a new profile group, which is hidden by default. */
explicit ProfileGroup(const Profile::Ptr &profileParent = Profile::Ptr());
/** Add a profile to the group. Calling setProperty() will update this
* profile. When creating a group, add the profiles to the group then
* call updateValues() to make the group's property values reflect the
* profiles currently in the group.
*/
void addProfile(const Profile::Ptr &profile);
/** Remove a profile from the group. Calling setProperty() will no longer
* affect this profile. */
void removeProfile(const Profile::Ptr &profile);
/** Returns the profiles in this group .*/
QList<Profile::Ptr> profiles() const;
/**
* Updates the property values in this ProfileGroup to match those from
* the group's profiles()
*
* For each available property, if each profile in the group has the same
* value then the ProfileGroup will use that value for the property.
* Otherwise the value for the property will be set to a null QVariant
*
* Some properties such as the name and the path of the profile
* will always be set to null if the group has more than one profile.
*/
void updateValues();
/** Sets the value of @p property in each of the group's profiles to
* @p value.
*/
void setProperty(Property p, const QVariant &value) override;
private:
Q_DISABLE_COPY(ProfileGroup)
QList<Profile::Ptr> _profiles;
};
inline ProfileGroup::ProfileGroup(const Profile::Ptr &profileParent)
: Profile(profileParent)
, _profiles(QList<Profile::Ptr>())
{
setHidden(true);
}
}
#endif
......@@ -41,6 +41,7 @@
// Konsole
#include "ProfileReader.h"
#include "ProfileWriter.h"
#include "ProfileGroup.h"
using namespace Konsole;
......
......@@ -35,6 +35,7 @@
// Konsole
#include "session/Session.h"
#include "profile/ProfileManager.h"
#include "ProfileCommandParser.h"
#include "history/HistoryTypeNone.h"
#include "history/HistoryTypeFile.h"
#include "history/compact/CompactHistoryType.h"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment