File indexing completed on 2024-12-22 05:17:15

0001 /*
0002  * This file is part of the KDE wacomtablet project. For copyright
0003  * information and license terms see the AUTHORS and COPYING files
0004  * in the top-level directory of this distribution.
0005  *
0006  * This program is free software; you can redistribute it and/or
0007  * modify it under the terms of the GNU General Public License as
0008  * published by the Free Software Foundation; either version 2 of
0009  * the License, or (at your option) any later version.
0010  *
0011  * This program is distributed in the hope that it will be useful,
0012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014  * GNU General Public License for more details.
0015  *
0016  * You should have received a copy of the GNU General Public License
0017  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0018  */
0019 
0020 #include "screenmap.h"
0021 
0022 #include "stringutils.h"
0023 
0024 #include <QHash>
0025 #include <QStringList>
0026 
0027 using namespace Wacom;
0028 
0029 namespace Wacom
0030 {
0031 class ScreenMapPrivate
0032 {
0033 public:
0034     static const QString SCREENAREA_SEPERATOR;
0035     static const QString SCREEN_SEPERATOR;
0036 
0037     TabletArea tabletGeometry;
0038     QHash<QString, TabletArea> mappings;
0039 };
0040 
0041 const QString ScreenMapPrivate::SCREENAREA_SEPERATOR = QLatin1String(":");
0042 const QString ScreenMapPrivate::SCREEN_SEPERATOR = QLatin1String("|");
0043 }
0044 
0045 ScreenMap::ScreenMap(const TabletArea &tabletGeometry)
0046     : d_ptr(new ScreenMapPrivate)
0047 {
0048     Q_D(ScreenMap);
0049 
0050     d->tabletGeometry = tabletGeometry;
0051 }
0052 
0053 ScreenMap::ScreenMap(const QString &mapping)
0054     : d_ptr(new ScreenMapPrivate)
0055 {
0056     fromString(mapping);
0057 }
0058 
0059 ScreenMap::ScreenMap(const ScreenMap &screenMap)
0060     : d_ptr(new ScreenMapPrivate)
0061 {
0062     operator=(screenMap);
0063 }
0064 
0065 ScreenMap::~ScreenMap()
0066 {
0067     delete this->d_ptr;
0068 }
0069 
0070 ScreenMap &ScreenMap::operator=(const ScreenMap &screenMap)
0071 {
0072     *(this->d_ptr) = *(screenMap.d_ptr);
0073 
0074     return (*this);
0075 }
0076 
0077 void ScreenMap::fromString(const QString &mappings)
0078 {
0079     Q_D(ScreenMap);
0080 
0081     QStringList screenMappings = mappings.split(QLatin1String("|"), Qt::SkipEmptyParts);
0082     QString separator(QLatin1String(":"));
0083     QStringList mapping;
0084     ScreenSpace screen;
0085     TabletArea tabletArea;
0086 
0087     d->mappings.clear();
0088 
0089     foreach (QString screenMapping, screenMappings) {
0090         mapping = screenMapping.split(separator, Qt::SkipEmptyParts);
0091 
0092         if (mapping.count() != 2) {
0093             continue;
0094         }
0095 
0096         screen = ScreenSpace(mapping.at(0).trimmed());
0097         tabletArea = TabletArea(mapping.at(1).trimmed());
0098 
0099         setMapping(screen, tabletArea);
0100     }
0101 }
0102 
0103 const TabletArea ScreenMap::getMapping(const ScreenSpace &screen) const
0104 {
0105     Q_D(const ScreenMap);
0106 
0107     // try to find selection for the current screen
0108     auto citer = d->mappings.constFind(screen.toString());
0109 
0110     TabletArea area;
0111 
0112     if (citer != d->mappings.constEnd()) {
0113         area = citer.value();
0114     } else {
0115         area = d->tabletGeometry;
0116     }
0117 
0118     return area;
0119 }
0120 
0121 const QString ScreenMap::getMappingAsString(const ScreenSpace &screen) const
0122 {
0123     return getMapping(screen).toString();
0124 }
0125 
0126 void ScreenMap::setMapping(const ScreenSpace &screen, const TabletArea &mapping)
0127 {
0128     Q_D(ScreenMap);
0129 
0130     if (mapping.isEmpty()) {
0131         d->mappings.insert(screen.toString(), d->tabletGeometry);
0132     } else {
0133         d->mappings.insert(screen.toString(), mapping);
0134     }
0135 }
0136 
0137 const QString ScreenMap::toString() const
0138 {
0139     Q_D(const ScreenMap);
0140 
0141     // create mapping string
0142     auto mapping = d->mappings.constBegin();
0143 
0144     QString mappings;
0145     TabletArea area;
0146 
0147     for (; mapping != d->mappings.constEnd(); ++mapping) {
0148         area = mapping.value();
0149 
0150         if (!mappings.isEmpty()) {
0151             mappings.append(ScreenMapPrivate::SCREEN_SEPERATOR);
0152         }
0153 
0154         mappings.append(QString::fromLatin1("%1%2%3").arg(mapping.key()).arg(ScreenMapPrivate::SCREENAREA_SEPERATOR).arg(area.toString()));
0155     }
0156 
0157     return mappings;
0158 }