1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
|
/*
Q Light Controller Plus
fixturebrowser.cpp
Copyright (c) Massimo Callegari
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0.txt
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 <QQmlEngine>
#include <QQuickItem>
#include <QQmlContext>
#include <QDebug>
#include "fixturebrowser.h"
#include "qlcfixturemode.h"
#include "qlcfixturedef.h"
#include "treemodelitem.h"
#include "treemodel.h"
#include "doc.h"
FixtureBrowser::FixtureBrowser(QQuickView *view, Doc *doc, QObject *parent)
: QObject(parent)
, m_doc(doc)
, m_view(view)
, m_manufacturerIndex(0)
, m_selectedManufacturer(QString())
, m_selectedModel(QString())
, m_fixtureName(QString())
, m_selectedMode(QString())
, m_modeChannelsCount(1)
, m_mode(nullptr)
, m_searchFilter(QString())
{
Q_ASSERT(m_doc != nullptr);
Q_ASSERT(m_view != nullptr);
m_view->rootContext()->setContextProperty("fixtureBrowser", this);
m_searchTree = new TreeModel(this);
QQmlEngine::setObjectOwnership(m_searchTree, QQmlEngine::CppOwnership);
m_searchTree->enableSorting(true);
}
FixtureBrowser::~FixtureBrowser()
{
m_view->rootContext()->setContextProperty("fixtureBrowser", nullptr);
}
QStringList FixtureBrowser::manufacturers()
{
if (m_defCache.isEmpty())
m_defCache = m_doc->fixtureDefCache()->fixtureCache();
QStringList mfList = m_defCache.keys();
mfList.sort(Qt::CaseInsensitive);
m_manufacturerIndex = mfList.indexOf("Generic");
emit manufacturerIndexChanged(m_manufacturerIndex);
return mfList;
}
QString FixtureBrowser::selectedManufacturer() const
{
return m_selectedManufacturer;
}
void FixtureBrowser::setSelectedManufacturer(QString selectedManufacturer)
{
if (m_selectedManufacturer == selectedManufacturer)
return;
m_selectedManufacturer = selectedManufacturer;
emit selectedManufacturerChanged(selectedManufacturer);
emit modelsListChanged();
}
QStringList FixtureBrowser::modelsList()
{
if (m_selectedManufacturer.isEmpty())
return QStringList();
if (m_defCache.isEmpty())
m_defCache = m_doc->fixtureDefCache()->fixtureCache();
qDebug() << "[FixtureBrowser] Models list for" << m_selectedManufacturer;
QStringList fxList = m_defCache[m_selectedManufacturer].keys();
if (m_selectedManufacturer == "Generic")
{
fxList << "Generic Dimmer";
fxList << "Generic RGB Panel";
}
fxList.sort(Qt::CaseInsensitive);
return fxList;
}
bool FixtureBrowser::isUserDefinition(QString manufacturer, QString model)
{
return m_defCache[manufacturer].value(model, false);
}
QString FixtureBrowser::selectedModel() const
{
return m_selectedModel;
}
void FixtureBrowser::setSelectedModel(QString selectedModel)
{
if (m_selectedModel == selectedModel)
return;
m_selectedModel = selectedModel;
setFixtureName(m_selectedModel);
m_selectedMode = QString();
m_modeChannelsCount = 1;
emit selectedModelChanged(selectedModel);
emit modesListChanged();
emit modeChannelsCountChanged();
emit modeChannelListChanged();
}
QString FixtureBrowser::fixtureName() const
{
return m_fixtureName;
}
void FixtureBrowser::setFixtureName(QString fixtureName)
{
if (m_fixtureName == fixtureName)
return;
m_fixtureName = fixtureName;
emit fixtureNameChanged(fixtureName);
}
QStringList FixtureBrowser::modesList()
{
qDebug() << "[FixtureBrowser] Modes list for" << m_selectedManufacturer << m_selectedModel;
QStringList modesList;
QLCFixtureDef *definition = fixtureDefinition();
if (definition != nullptr)
{
QList<QLCFixtureMode *> fxModesList = definition->modes();
foreach (QLCFixtureMode *mode, fxModesList)
{
modesList.append(mode->name());
if (m_selectedMode.isEmpty())
{
m_selectedMode = mode->name();
m_modeChannelsCount = mode->channels().count();
}
}
}
return modesList;
}
QString FixtureBrowser::selectedMode() const
{
return m_selectedMode;
}
void FixtureBrowser::setSelectedMode(QString selectedMode)
{
qDebug() << "[FixtureBrowser] Select mode for" << m_selectedManufacturer << m_selectedModel << selectedMode;
if (m_selectedMode == selectedMode)
return;
m_selectedMode = selectedMode;
QLCFixtureDef *definition = fixtureDefinition();
if (definition != nullptr)
{
m_mode = definition->mode(m_selectedMode);
if (m_mode)
m_modeChannelsCount = m_mode->channels().count();
}
emit selectedModeChanged(selectedMode);
emit modeChannelsCountChanged();
emit modeChannelListChanged();
}
int FixtureBrowser::modeChannelsCount()
{
QLCFixtureDef *definition = fixtureDefinition();
if (definition != nullptr)
{
m_mode = definition->mode(m_selectedMode);
if (m_mode != nullptr)
return m_mode->channels().count();
}
return m_modeChannelsCount;
}
void FixtureBrowser::setModeChannelsCount(int modeChannelsCount)
{
if (m_modeChannelsCount == modeChannelsCount)
return;
m_modeChannelsCount = modeChannelsCount;
emit modeChannelsCountChanged();
}
QVariant FixtureBrowser::modeChannelList() const
{
QVariantList channelList;
if (m_mode != nullptr)
{
int i = 1;
for (QLCChannel *channel : m_mode->channels()) // C++11
{
QVariantMap chMap;
chMap.insert("mIcon", channel->getIconNameFromGroup(channel->group(), true));
chMap.insert("mLabel", QString("%1: %2").arg(i++).arg(channel->name()));
channelList.append(chMap);
}
}
return QVariant::fromValue(channelList);
}
int FixtureBrowser::manufacturerIndex() const
{
return m_manufacturerIndex;
}
void FixtureBrowser::setManufacturerIndex(int index)
{
if (m_manufacturerIndex == index)
return;
m_manufacturerIndex = index;
emit manufacturerIndexChanged(index);
}
int FixtureBrowser::availableChannel(quint32 uniIdx, int channels, int quantity, int gap, int requested)
{
qDebug() << "[FixtureBrowser] uniIdx:" << uniIdx << ", channels:" << channels << ", requested:" << requested;
bool isAvailable = true;
quint32 uniFilter = uniIdx == Universe::invalid() ? 0 : uniIdx;
quint32 absAddress = (requested & 0x01FF) | (uniFilter << 9);
for (int n = 0; n < quantity; n++)
{
for (int i = 0; i < channels; i++)
{
if (m_doc->fixtureForAddress(absAddress + i) != Fixture::invalidId())
{
isAvailable = false;
break;
}
}
absAddress += channels + gap;
}
if (isAvailable == true)
{
qDebug() << "[FixtureBrowser] Requested channel is available:" << requested;
return requested;
}
else
{
qDebug() << "[FixtureBrowser] Requested channel" << requested << "not available in universe" << uniFilter;
int validAddr = 0;
int freeCounter = 0;
absAddress = uniFilter << 9;
for (int i = 0; i < 512; i++)
{
if (m_doc->fixtureForAddress(absAddress + i) != Fixture::invalidId())
{
freeCounter = 0;
validAddr = i + 1;
}
else
freeCounter++;
if (freeCounter == (channels * quantity) + (gap * quantity))
{
qDebug() << "[FixtureBrowser] Returning available address:" << validAddr;
return validAddr;
}
}
}
return -1;
}
int FixtureBrowser::availableChannel(quint32 fixtureID, int requested)
{
qDebug() << "[FixtureBrowser] fxID:" << fixtureID << ", requested:" << requested;
bool isAvailable = true;
Fixture *fixture = m_doc->fixture(fixtureID);
if (fixture == nullptr)
return -1;
quint32 channels = fixture->channels();
quint32 absAddress = (requested & 0x01FF) | (fixture->universe() << 9);
for (quint32 i = 0; i < channels; i++)
{
quint32 fxIDOnAddr = m_doc->fixtureForAddress(absAddress + i);
if (fxIDOnAddr != Fixture::invalidId() && fxIDOnAddr != fixtureID)
{
isAvailable = false;
break;
}
}
if (isAvailable == true)
{
qDebug() << "[FixtureBrowser] Requested channel is available:" << requested;
return requested;
}
return -1;
}
QString FixtureBrowser::searchFilter() const
{
return m_searchFilter;
}
void FixtureBrowser::setSearchFilter(QString searchFilter)
{
if (m_searchFilter == searchFilter)
return;
if (m_searchFilter.length() >= SEARCH_MIN_CHARS && searchFilter.length() < SEARCH_MIN_CHARS)
{
m_selectedManufacturer = "";
emit selectedManufacturerChanged(m_selectedManufacturer);
}
m_searchFilter = searchFilter;
if (searchFilter.length() >= SEARCH_MIN_CHARS)
updateSearchTree();
else
{
m_searchTree->clear();
emit searchListChanged();
}
emit searchFilterChanged(searchFilter);
}
QVariant FixtureBrowser::searchTreeModel() const
{
return QVariant::fromValue(m_searchTree);
}
void FixtureBrowser::updateSearchTree()
{
m_searchTree->clear();
QStringList mfList = m_doc->fixtureDefCache()->manufacturers();
mfList.sort();
QString searchFilter = m_searchFilter.toLower();
for (QString &manufacturer : mfList) // C++11
{
QStringList modelsList = m_doc->fixtureDefCache()->models(manufacturer);
modelsList.sort();
for (QString &model : modelsList)
{
if (manufacturer.toLower().contains(searchFilter) ||
model.toLower().contains(searchFilter))
{
QVariantList params;
TreeModelItem *item = m_searchTree->addItem(model, params, manufacturer);
item->setFlag(TreeModel::Expanded, true);
}
}
}
emit searchListChanged();
}
QLCFixtureDef *FixtureBrowser::fixtureDefinition()
{
return m_doc->fixtureDefCache()->fixtureDef(m_selectedManufacturer, m_selectedModel);
}
|