2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2011-02-22 12:08:19 +01:00
|
|
|
**
|
2015-01-14 18:07:15 +01:00
|
|
|
** Copyright (C) 2015 The Qt Company Ltd.
|
|
|
|
** Contact: http://www.qt.io/licensing
|
2011-02-22 12:08:19 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2011-02-22 12:08:19 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** Commercial License Usage
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
2015-01-14 18:07:15 +01:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms and
|
|
|
|
** conditions see http://www.qt.io/terms-conditions. For further information
|
2014-10-01 13:21:18 +02:00
|
|
|
** use the contact form at http://www.qt.io/contact-us.
|
2011-02-22 12:08:19 +01:00
|
|
|
**
|
|
|
|
** GNU Lesser General Public License Usage
|
2012-10-02 09:12:39 +02:00
|
|
|
** Alternatively, this file may be used under the terms of the GNU Lesser
|
2014-10-01 13:21:18 +02:00
|
|
|
** General Public License version 2.1 or version 3 as published by the Free
|
|
|
|
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
|
|
|
|
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
|
|
|
|
** following information to ensure the GNU Lesser General Public License
|
|
|
|
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
|
|
|
|
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
2012-10-02 09:12:39 +02:00
|
|
|
**
|
2015-01-14 18:07:15 +01:00
|
|
|
** In addition, as a special exception, The Qt Company gives you certain additional
|
|
|
|
** rights. These rights are described in The Qt Company LGPL Exception
|
2011-02-22 12:08:19 +01:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2011-02-22 12:08:19 +01:00
|
|
|
|
2010-11-24 14:52:06 +01:00
|
|
|
#include "valueschangedcommand.h"
|
|
|
|
|
2014-07-31 16:45:21 +02:00
|
|
|
#include "sharedmemory.h"
|
2012-09-11 15:11:34 +02:00
|
|
|
#include <QCache>
|
2013-07-16 17:00:07 +02:00
|
|
|
#include <QDebug>
|
2012-09-11 15:11:34 +02:00
|
|
|
|
2012-09-20 14:25:01 +02:00
|
|
|
#include <cstring>
|
|
|
|
|
2010-11-24 14:52:06 +01:00
|
|
|
namespace QmlDesigner {
|
|
|
|
|
2014-08-05 14:00:21 +02:00
|
|
|
// using cache as a container which deletes sharedmemory pointers at process exit
|
|
|
|
typedef QCache<qint32, SharedMemory> GlobalSharedMemoryContainer;
|
|
|
|
Q_GLOBAL_STATIC_WITH_ARGS(GlobalSharedMemoryContainer, globalSharedMemoryContainer, (10000))
|
2012-09-11 15:11:34 +02:00
|
|
|
|
2010-11-24 14:52:06 +01:00
|
|
|
ValuesChangedCommand::ValuesChangedCommand()
|
2012-09-11 15:11:34 +02:00
|
|
|
: m_keyNumber(0)
|
2010-11-24 14:52:06 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ValuesChangedCommand::ValuesChangedCommand(const QVector<PropertyValueContainer> &valueChangeVector)
|
2012-09-11 15:11:34 +02:00
|
|
|
: m_valueChangeVector (valueChangeVector),
|
|
|
|
m_keyNumber(0)
|
2010-11-24 14:52:06 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
QVector<PropertyValueContainer> ValuesChangedCommand::valueChanges() const
|
|
|
|
{
|
|
|
|
return m_valueChangeVector;
|
|
|
|
}
|
|
|
|
|
2012-09-11 15:11:34 +02:00
|
|
|
quint32 ValuesChangedCommand::keyNumber() const
|
|
|
|
{
|
|
|
|
return m_keyNumber;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ValuesChangedCommand::removeSharedMemorys(const QVector<qint32> &keyNumberVector)
|
|
|
|
{
|
|
|
|
foreach (qint32 keyNumber, keyNumberVector) {
|
2014-08-05 14:00:21 +02:00
|
|
|
SharedMemory *sharedMemory = globalSharedMemoryContainer()->take(keyNumber);
|
2012-09-11 15:11:34 +02:00
|
|
|
delete sharedMemory;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-04 17:06:31 +02:00
|
|
|
void ValuesChangedCommand::sort()
|
|
|
|
{
|
|
|
|
qSort(m_valueChangeVector);
|
|
|
|
}
|
|
|
|
|
2012-09-11 15:11:34 +02:00
|
|
|
static const QLatin1String valueKeyTemplateString("Values-%1");
|
|
|
|
|
2014-07-31 16:45:21 +02:00
|
|
|
static SharedMemory *createSharedMemory(qint32 key, int byteCount)
|
2012-09-11 15:11:34 +02:00
|
|
|
{
|
2014-07-31 16:45:21 +02:00
|
|
|
SharedMemory *sharedMemory = new SharedMemory(QString(valueKeyTemplateString).arg(key));
|
2012-09-11 15:11:34 +02:00
|
|
|
|
|
|
|
bool sharedMemoryIsCreated = sharedMemory->create(byteCount);
|
|
|
|
|
|
|
|
if (sharedMemoryIsCreated) {
|
2014-08-05 14:00:21 +02:00
|
|
|
globalSharedMemoryContainer()->insert(key, sharedMemory);
|
2012-09-11 15:11:34 +02:00
|
|
|
return sharedMemory;
|
2014-08-05 14:00:21 +02:00
|
|
|
} else {
|
|
|
|
delete sharedMemory;
|
2012-09-11 15:11:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-11-24 14:52:06 +01:00
|
|
|
QDataStream &operator<<(QDataStream &out, const ValuesChangedCommand &command)
|
|
|
|
{
|
2012-09-11 17:25:27 +02:00
|
|
|
static const bool dontUseSharedMemory = !qgetenv("DESIGNER_DONT_USE_SHARED_MEMORY").isEmpty();
|
|
|
|
|
|
|
|
if (!dontUseSharedMemory && command.valueChanges().count() > 5) {
|
2012-09-11 15:11:34 +02:00
|
|
|
static quint32 keyCounter = 0;
|
|
|
|
++keyCounter;
|
|
|
|
command.m_keyNumber = keyCounter;
|
|
|
|
QByteArray outDataStreamByteArray;
|
|
|
|
QDataStream temporaryOutDataStream(&outDataStreamByteArray, QIODevice::WriteOnly);
|
2012-09-20 11:30:11 +02:00
|
|
|
temporaryOutDataStream.setVersion(QDataStream::Qt_4_8);
|
2012-09-11 15:11:34 +02:00
|
|
|
|
2013-09-03 18:48:02 +02:00
|
|
|
temporaryOutDataStream << command.valueChanges();
|
2012-09-11 15:11:34 +02:00
|
|
|
|
2014-07-31 16:45:21 +02:00
|
|
|
SharedMemory *sharedMemory = createSharedMemory(keyCounter, outDataStreamByteArray.size());
|
2012-09-11 15:11:34 +02:00
|
|
|
|
|
|
|
if (sharedMemory) {
|
2014-07-17 15:57:09 +02:00
|
|
|
sharedMemory->lock();
|
2012-09-20 14:25:01 +02:00
|
|
|
std::memcpy(sharedMemory->data(), outDataStreamByteArray.constData(), sharedMemory->size());
|
2014-07-17 15:57:09 +02:00
|
|
|
sharedMemory->unlock();
|
2014-07-31 16:45:21 +02:00
|
|
|
|
2012-09-11 15:11:34 +02:00
|
|
|
out << command.keyNumber();
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out << qint32(0);
|
2010-11-24 14:52:06 +01:00
|
|
|
out << command.valueChanges();
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2012-09-11 15:11:34 +02:00
|
|
|
void readSharedMemory(qint32 key, QVector<PropertyValueContainer> *valueChangeVector)
|
|
|
|
{
|
2014-07-31 16:45:21 +02:00
|
|
|
SharedMemory sharedMemory(QString(valueKeyTemplateString).arg(key));
|
2012-09-11 15:11:34 +02:00
|
|
|
bool canAttach = sharedMemory.attach(QSharedMemory::ReadOnly);
|
|
|
|
|
|
|
|
if (canAttach) {
|
2014-07-31 16:45:21 +02:00
|
|
|
sharedMemory.lock();
|
|
|
|
|
2012-09-11 15:11:34 +02:00
|
|
|
QDataStream in(QByteArray::fromRawData(static_cast<const char*>(sharedMemory.constData()), sharedMemory.size()));
|
2012-09-20 11:30:11 +02:00
|
|
|
in.setVersion(QDataStream::Qt_4_8);
|
2012-09-11 15:11:34 +02:00
|
|
|
in >> *valueChangeVector;
|
2014-07-31 16:45:21 +02:00
|
|
|
|
|
|
|
sharedMemory.unlock();
|
|
|
|
sharedMemory.detach();
|
2012-09-11 15:11:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-24 14:52:06 +01:00
|
|
|
QDataStream &operator>>(QDataStream &in, ValuesChangedCommand &command)
|
|
|
|
{
|
2012-09-11 15:11:34 +02:00
|
|
|
in >> command.m_keyNumber;
|
2010-11-24 14:52:06 +01:00
|
|
|
|
2012-09-11 15:11:34 +02:00
|
|
|
if (command.keyNumber() > 0) {
|
|
|
|
readSharedMemory(command.keyNumber(), &command.m_valueChangeVector);
|
|
|
|
} else {
|
|
|
|
in >> command.m_valueChangeVector;
|
|
|
|
}
|
2010-11-24 14:52:06 +01:00
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
2013-07-04 17:06:31 +02:00
|
|
|
bool operator ==(const ValuesChangedCommand &first, const ValuesChangedCommand &second)
|
|
|
|
{
|
|
|
|
return first.m_valueChangeVector == second.m_valueChangeVector;
|
|
|
|
}
|
|
|
|
|
2013-07-16 17:00:07 +02:00
|
|
|
QDebug operator <<(QDebug debug, const ValuesChangedCommand &command)
|
|
|
|
{
|
|
|
|
return debug.nospace() << "ValuesChangedCommand("
|
|
|
|
<< "keyNumber: " << command.keyNumber() << ", "
|
|
|
|
<< command.valueChanges() << ")";
|
|
|
|
}
|
|
|
|
|
2010-11-24 14:52:06 +01:00
|
|
|
} // namespace QmlDesigner
|