Debian Codegraph @

src/Gui/Dialogs/DlgCustomizeSpaceball.cpp

raw
1 /***************************************************************************
2 * Copyright (c) 2011 Thomas Anderson <ta@nextgenengineering> *
3 * *
4 * This file is part of the FreeCAD CAx development system. *
5 * *
6 * This library is free software; you can redistribute it and/or *
7 * modify it under the terms of the GNU Library General Public *
8 * License as published by the Free Software Foundation; either *
9 * version 2 of the License, or (at your option) any later version. *
10 * *
11 * This library is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU Library General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Library General Public *
17 * License along with this library; see the file COPYING.LIB. If not, *
18 * write to the Free Software Foundation, Inc., 59 Temple Place, *
19 * Suite 330, Boston, MA 02111-1307, USA *
20 * *
21 ***************************************************************************/
22
23 #include <QComboBox>
24 #include <QHBoxLayout>
25 #include <QHeaderView>
26 #include <QLabel>
27 #include <QPainter>
28 #include <QPrintDialog>
29 #include <QPrinter>
30 #include <QPushButton>
31 #include <QSplitter>
32 #include <QTableView>
33 #include <QVBoxLayout>
34
35 #include "Base/Console.h"
36
37 #include "Command.h"
38 #include "Dialogs/DlgCustomizeSpaceball.h"
39 #include "Application.h"
40 #include "BitmapFactory.h"
41 #include "GuiApplicationNativeEventAware.h"
42 #include "SpaceballEvent.h"
43
44
45 using GroupVector = std::vector<Base::Reference<ParameterGrp>>;
46
47 using namespace Gui::Dialog;
48
49 ButtonView::ButtonView(QWidget* parent)
50 : QListView(parent)
51 {}
52
53 void ButtonView::selectButton(int number)
54 {
55 this->selectionModel()->select(this->model()->index(number, 0), QItemSelectionModel::ClearAndSelect);
56 this->scrollTo(this->model()->index(number, 0), QAbstractItemView::EnsureVisible);
57 }
58
59 void ButtonView::goSelectionChanged(const QItemSelection& selected, const QItemSelection& deselected)
60 {
61 Q_UNUSED(deselected);
62 if (selected.indexes().isEmpty()) {
63 return;
64 }
65 QModelIndex select(selected.indexes().at(0));
66 Q_EMIT changeCommandSelection(this->model()->data(select, Qt::UserRole).toString());
67 }
68
69 void ButtonView::goChangedCommand(const QString& commandName)
70 {
71 QModelIndex index(this->currentIndex());
72 auto model = dynamic_cast<ButtonModel*>(this->model());
73 if (model && index.isValid()) {
74 model->setCommand(index.row(), commandName);
75 }
76 }
77
78 ///////////////////////////////////////////////////////////////////////////////////////
79
80 ButtonModel::ButtonModel(QObject* parent)
81 : QAbstractListModel(parent)
82 {
83 // load3DConnexionButtons("SpacePilot Pro");
84 }
85
86 // Process the given Mapping tree to load in the Button mappings.
87 void ButtonModel::load3DConnexionButtonMapping(boost::property_tree::ptree ButtonMapTree)
88 {
89 spaceballButtonGroup()->Clear();
90
91 BOOST_FOREACH (
92 const boost::property_tree::ptree::value_type& Map,
93 ButtonMapTree.get_child("Mapping")
94 ) {
95 if ("Map" == Map.first) {
96 std::string ButtonDescription;
97 std::string ButtonCode;
98 std::string ButtonCommand;
99 std::string ButtonDownTime;
100
101 // Inspect Map attributes
102 BOOST_FOREACH (
103 const boost::property_tree::ptree::value_type& kv,
104 Map.second.get_child("<xmlattr>")
105 ) {
106 std::string Attribute;
107 std::string Value;
108
109 Attribute = kv.first.data();
110 Value = kv.second.data();
111
112 if (0 == Attribute.compare("Description")) {
113 ButtonDescription = Value;
114 }
115 if (0 == Attribute.compare("KeyCode")) {
116 ButtonCode = Value;
117 }
118 if (0 == Attribute.compare("DownTime")) {
119 ButtonDownTime = Value;
120 }
121 if (0 == Attribute.compare("Command")) {
122 ButtonCommand = Value;
123 }
124 }
125
126 // ButtonCode is mandatory, the remaining attributes optional.
127 if (!ButtonCode.empty()) {
128 Base::Reference<ParameterGrp> newGroup;
129
130 newGroup = spaceballButtonGroup()->GetGroup(ButtonCode.c_str());
131 newGroup->SetASCII("Command", ButtonCommand.c_str());
132 newGroup->SetASCII("Description", ButtonDescription.c_str());
133 }
134 }
135 }
136 }
137
138 // Optionally preload Button model with 3DConnexion configuration to match Solidworks
139 // For now the Button mapping file (3DConnexion.xml) is held the same folder as the FreeCAD executable.
140 void ButtonModel::load3DConnexionButtons(const char* RequiredDeviceName)
141 {
142 try {
143 boost::property_tree::ptree tree;
144 boost::property_tree::ptree DeviceTree;
145
146 // exception thrown if no file found
147 std::string path = App::Application::getResourceDir();
148 path += "3Dconnexion/3DConnexion.xml";
149 read_xml(path.c_str(), tree);
150
151 BOOST_FOREACH (const boost::property_tree::ptree::value_type& ButtonMap, tree.get_child("")) {
152 if ("ButtonMap" == ButtonMap.first) {
153 // Inspect ButtonMap attributes for DeviceName
154 BOOST_FOREACH (
155 const boost::property_tree::ptree::value_type& kv,
156 ButtonMap.second.get_child("<xmlattr>")
157 ) {
158 std::string Attribute;
159 std::string Value;
160
161 Attribute = kv.first.data();
162 Value = kv.second.data();
163
164 if (0 == Attribute.compare("DeviceName")) {
165 if (0 == Value.compare(RequiredDeviceName)) {
166 // We found the ButtonMap we want to load up
167 DeviceTree = ButtonMap.second;
168 }
169 }
170 }
171 }
172 }
173 // If we found the required devices ButtonMap
174 if (!DeviceTree.empty()) {
175 load3DConnexionButtonMapping(DeviceTree);
176 }
177 }
178 catch (const std::exception& e) {
179 // We don't mind not finding the file to be opened
180 Base::Console().warning("%s\n", e.what());
181 }
182 }
183
184 int ButtonModel::rowCount(const QModelIndex& parent) const
185 {
186 Q_UNUSED(parent);
187 return spaceballButtonGroup()->GetGroups().size();
188 }
189
190 QVariant ButtonModel::data(const QModelIndex& index, int role) const
191 {
192 GroupVector groupVector = spaceballButtonGroup()->GetGroups();
193 if (index.row() >= (int)groupVector.size()) {
194 Base::Console().log("index error in ButtonModel::data\n");
195 return {};
196 }
197 if (role == Qt::DisplayRole) {
198 return {getLabel(index.row())};
199 }
200 if (role == Qt::DecorationRole) {
201 static QPixmap icon(
202 BitmapFactory()
203 .pixmap("spaceball_button")
204 .scaled(32, 32, Qt::IgnoreAspectRatio, Qt::SmoothTransformation)
205 );
206 return QVariant(QIcon(icon)); // Cannot make a QPixmap into a QVariant, so convert to a
207 // QIcon first
208 }
209 if (role == Qt::UserRole) {
210 return {QString::fromStdString(groupVector.at(index.row())->GetASCII("Command"))};
211 }
212 if (role == Qt::SizeHintRole) {
213 return {QSize(32, 32)};
214 }
215 return {};
216 }
217
218 void ButtonModel::insertButtonRows(int number)
219 {
220 int buttonCount = spaceballButtonGroup()->GetGroups().size();
221 beginInsertRows(QModelIndex(), buttonCount, number - buttonCount + 1);
222 for (int index = buttonCount; index < number + 1; ++index) {
223 QString groupName;
224 groupName.setNum(index);
225 Base::Reference<ParameterGrp> newGroup = spaceballButtonGroup()->GetGroup(
226 groupName.toLatin1()
227 ); // builds the group.
228 newGroup->SetASCII("Command", "");
229 newGroup->SetASCII("Description", "");
230 }
231 endInsertRows();
232 return;
233 }
234
235 void ButtonModel::setCommand(int row, QString command)
236 {
237 GroupVector groupVector = spaceballButtonGroup()->GetGroups();
238 groupVector.at(row)->SetASCII("Command", command.toLatin1());
239 }
240
241 void ButtonModel::goButtonPress(int number)
242 {
243 QString numberString;
244 numberString.setNum(number);
245 if (!spaceballButtonGroup()->HasGroup(numberString.toLatin1())) {
246 insertButtonRows(number);
247 }
248 }
249
250 void ButtonModel::goMacroRemoved(const QByteArray& macroName)
251 {
252 GroupVector groupVector = spaceballButtonGroup()->GetGroups();
253 for (auto& it : groupVector) {
254 if (std::string(macroName.data()) == it->GetASCII("Command")) {
255 it->SetASCII("Command", "");
256 }
257 }
258 }
259
260 void ButtonModel::goClear()
261 {
262 if (this->rowCount() < 1) {
263 return;
264 }
265 this->beginRemoveRows(QModelIndex(), 0, this->rowCount() - 1);
266 spaceballButtonGroup()->Clear();
267 this->endRemoveRows();
268 }
269
270 ParameterGrp::handle ButtonModel::spaceballButtonGroup() const
271 {
272 static ParameterGrp::handle group = App::GetApplication()
273 .GetUserParameter()
274 .GetGroup("BaseApp")
275 ->GetGroup("Spaceball")
276 ->GetGroup("Buttons");
277 return group;
278 }
279
280 QString ButtonModel::getLabel(const int& number) const
281 {
282 if (number > -1 && number < 32) {
283 QString numberString;
284 numberString.setNum(number);
285 QString desc = QString::fromStdString(
286 spaceballButtonGroup()->GetGroup(numberString.toLatin1())->GetASCII("Description", "")
287 );
288 if (desc.length()) {
289 desc = QStringLiteral(" \"") + desc + QStringLiteral("\"");
290 }
291 return tr("Button %1").arg(number + 1) + desc;
292 }
293 else {
294 return tr("Out of range");
295 }
296 }
297
298 void ButtonModel::loadConfig(const char* RequiredDeviceName)
299 {
300 goClear();
301 if (!RequiredDeviceName) {
302 return;
303 }
304 load3DConnexionButtons(RequiredDeviceName);
305 }
306
307 //////////////////////////////////////////////////////////////////////////////////////////
308
309 CommandView::CommandView(QWidget* parent)
310 : QTreeView(parent)
311 {
312 this->setEnabled(false);
313 connect(this, &QTreeView::clicked, this, &CommandView::goClicked);
314 }
315
316 void CommandView::goChangeCommandSelection(const QString& commandName)
317 {
318 if (!this->isEnabled()) {
319 this->setEnabled(true);
320 }
321 this->selectionModel()->clear();
322 this->collapseAll();
323 if (commandName.isEmpty()) {
324 return;
325 }
326 QModelIndexList index(this->model()->match(
327 this->model()->index(0, 0),
328 Qt::UserRole,
329 QVariant(commandName),
330 1,
331 Qt::MatchWrap | Qt::MatchRecursive
332 ));
333 if (index.empty()) {
334 return;
335 }
336 this->expand(index.at(0));
337 this->setCurrentIndex(index.at(0));
338 }
339
340 void CommandView::goClicked(const QModelIndex& index)
341 {
342 if (index.flags() & Qt::ItemIsSelectable) {
343 QString commandName = this->model()->data(index, Qt::UserRole).toString();
344 if (commandName.isEmpty()) {
345 return;
346 }
347 Q_EMIT changedCommand(commandName);
348 }
349 }
350
351 /////////////////////////////////////////////////////////////////////////////////////////
352
353 CommandNode::CommandNode(NodeType typeIn)
354 {
355 // NOLINTBEGIN
356 nodeType = typeIn;
357 parent = nullptr;
358 children.clear();
359 aCommand = nullptr;
360 // NOLINTEND
361 }
362
363 CommandNode::~CommandNode()
364 {
365 qDeleteAll(children);
366 }
367
368 /////////////////////////////////////////////////////////////////////////////////////////
369
370 CommandModel::CommandModel(QObject* parent)
371 : QAbstractItemModel(parent)
372 {
373 // NOLINTBEGIN
374 rootNode = nullptr;
375 initialize();
376 // NOLINTEND
377 }
378
379 CommandModel::~CommandModel()
380 {
381 delete rootNode;
382 rootNode = nullptr;
383 }
384
385 QModelIndex CommandModel::index(int row, int column, const QModelIndex& parent) const
386 {
387 if (!rootNode) {
388 return {};
389 }
390 if (!parent.isValid()) {
391 return createIndex(row, column, rootNode->children.at(row));
392 }
393
394 CommandNode* parentNode = nodeFromIndex(parent);
395 if (!parentNode) {
396 return {};
397 }
398 return createIndex(row, column, parentNode->children.at(row));
399 }
400
401 QModelIndex CommandModel::parent(const QModelIndex& index) const
402 {
403 CommandNode* base = nodeFromIndex(index);
404 if (!base) {
405 return {};
406 }
407 CommandNode* parentNode = base->parent;
408 if (!parentNode) {
409 return {};
410 }
411 CommandNode* grandParentNode = parentNode->parent;
412 if (!grandParentNode) {
413 return {};
414 }
415
416 int row = grandParentNode->children.indexOf(parentNode);
417 if (row == -1) {
418 return {};
419 }
420 return createIndex(row, index.column(), parentNode);
421 }
422
423 int CommandModel::rowCount(const QModelIndex& parent) const
424 {
425 if (!parent.isValid()) {
426 return rootNode->children.size();
427 }
428
429 CommandNode* parentNode = nodeFromIndex(parent);
430 if (!parentNode) {
431 return 0;
432 }
433 return parentNode->children.count();
434 }
435
436 int CommandModel::columnCount(const QModelIndex& parent) const
437 {
438 Q_UNUSED(parent);
439 return 1;
440 }
441
442 QVariant CommandModel::data(const QModelIndex& index, int role) const
443 {
444 CommandNode* node = nodeFromIndex(index);
445 if (!node) {
446 return {};
447 }
448 if (role == Qt::DisplayRole) {
449 if (node->nodeType == CommandNode::CommandType) {
450 return {qApp->translate(node->aCommand->className(), node->aCommand->getMenuText())};
451 }
452 if (node->nodeType == CommandNode::GroupType) {
453 if (node->children.empty()) {
454 return {};
455 }
456 CommandNode* childNode = node->children.at(0);
457 return {
458 qApp->translate(childNode->aCommand->className(), childNode->aCommand->getGroupName())
459 };
460 }
461 return {};
462 }
463 if (role == Qt::DecorationRole) {
464 if (node->nodeType == CommandNode::CommandType) {
465 if (node->aCommand->getPixmap()) {
466 return QVariant(
467 BitmapFactory()
468 .pixmap(node->aCommand->getPixmap())
469 .scaled(32, 32, Qt::IgnoreAspectRatio, Qt::SmoothTransformation)
470 );
471 }
472 }
473 }
474 if (role == Qt::SizeHintRole) {
475 if (node->nodeType == CommandNode::CommandType) {
476 return {QSize(32, 32)};
477 }
478 }
479 if (role == Qt::UserRole) {
480 if (node->nodeType == CommandNode::CommandType) {
481 return {QString::fromLatin1(node->aCommand->getName())};
482 }
483 if (node->nodeType == CommandNode::GroupType) {
484 if (node->children.empty()) {
485 return {};
486 }
487 CommandNode* childNode = node->children.at(0);
488 return {QString::fromLatin1(childNode->aCommand->getGroupName())};
489 }
490 return {};
491 }
492 if (role == Qt::ToolTipRole) {
493 if (node->nodeType == CommandNode::CommandType) {
494 return {QString::fromLatin1(node->aCommand->getToolTipText())};
495 }
496 }
497 return {};
498 }
499
500 QVariant CommandModel::headerData(int section, Qt::Orientation orientation, int role) const
501 {
502 if (role == Qt::DisplayRole && orientation == Qt::Horizontal && section == 0) {
503 return {tr("Commands")};
504 }
505 return {};
506 }
507
508 Qt::ItemFlags CommandModel::flags(const QModelIndex& index) const
509 {
510 if (!index.isValid()) {
511 return Qt::NoItemFlags;
512 }
513 CommandNode* node = nodeFromIndex(index);
514 if (!node) {
515 return Qt::NoItemFlags;
516 }
517 if (node->nodeType == CommandNode::CommandType) {
518 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
519 }
520 return Qt::NoItemFlags;
521 }
522
523 CommandNode* CommandModel::nodeFromIndex(const QModelIndex& index) const
524 {
525 if (index.isValid()) {
526 return static_cast<CommandNode*>(index.internalPointer());
527 }
528 return rootNode;
529 }
530
531 void CommandModel::goAddMacro(const QByteArray& macroName)
532 {
533 QModelIndexList indexList(this->match(
534 this->index(0, 0),
535 Qt::UserRole,
536 QVariant(QStringLiteral("Macros")),
537 1,
538 Qt::MatchWrap | Qt::MatchRecursive
539 ));
540 QModelIndex macrosIndex;
541 if (indexList.empty()) {
542 // this is the first macro and we have to add the Macros item.
543 // figure out where to insert it. Should be in the command groups now.
544 QStringList groups = orderedGroups();
545 int location(groups.indexOf(QStringLiteral("Macros")));
546 if (location == -1) {
547 location = groups.size();
548 }
549 // add row
550 this->beginInsertRows(QModelIndex(), location, location);
551 auto macroNode = new CommandNode(CommandNode::GroupType);
552 macroNode->parent = rootNode;
553 rootNode->children.insert(location, macroNode);
554 this->endInsertRows();
555 macrosIndex = this->index(location, 0);
556 }
557 else {
558 macrosIndex = indexList.at(0);
559 }
560
561 Command* command = nullptr;
562 command = Application::Instance->commandManager().getCommandByName(macroName);
563 if (!command) {
564 return;
565 }
566
567 CommandNode* parentNode = nodeFromIndex(macrosIndex);
568 if (!parentNode) {
569 return;
570 }
571
572 this->beginInsertRows(macrosIndex, parentNode->children.size(), parentNode->children.size());
573 auto childNode = new CommandNode(CommandNode::CommandType);
574 childNode->parent = parentNode;
575 parentNode->children.push_back(childNode);
576 childNode->aCommand = command;
577 this->endInsertRows();
578 }
579
580 void CommandModel::goRemoveMacro(const QByteArray& macroName)
581 {
582 QModelIndexList macroList(this->match(
583 this->index(0, 0),
584 Qt::UserRole,
585 QVariant(QString::fromLatin1(macroName.data())),
586 1,
587 Qt::MatchWrap | Qt::MatchRecursive
588 ));
589 if (macroList.isEmpty()) {
590 return;
591 }
592
593 QModelIndex childIndex(macroList.at(0));
594 QModelIndex parentIndex(this->parent(childIndex));
595 if (!childIndex.isValid() || !parentIndex.isValid()) {
596 return;
597 }
598
599 CommandNode* parentNode = nodeFromIndex(parentIndex);
600 if (!parentNode) {
601 return;
602 }
603
604 this->beginRemoveRows(parentIndex, childIndex.row(), childIndex.row());
605 delete parentNode->children.takeAt(childIndex.row());
606 this->endRemoveRows();
607 if (parentNode->children.isEmpty()) {
608 QModelIndex grandParentIndex(this->parent(parentIndex)); // this should be root.
609 CommandNode* grandParentNode = nodeFromIndex(grandParentIndex);
610 this->beginRemoveRows(grandParentIndex, parentIndex.row(), parentIndex.row());
611 delete grandParentNode->children.takeAt(parentIndex.row());
612 this->endRemoveRows();
613 }
614 }
615
616 void CommandModel::initialize()
617 {
618 rootNode = new CommandNode(CommandNode::RootType);
619 QStringList groups(orderedGroups());
620 for (const auto& group : groups) {
621 groupCommands(group);
622 }
623 }
624
625 void CommandModel::groupCommands(const QString& groupName)
626 {
627 auto parentNode = new CommandNode(CommandNode::GroupType);
628 parentNode->parent = rootNode;
629 rootNode->children.push_back(parentNode);
630 std::vector<Command*> commands = Application::Instance->commandManager().getGroupCommands(
631 groupName.toLatin1()
632 );
633 for (const auto& command : commands) {
634 auto childNode = new CommandNode(CommandNode::CommandType);
635 childNode->parent = parentNode;
636 parentNode->children.push_back(childNode);
637 childNode->aCommand = command;
638 }
639 }
640
641 QStringList CommandModel::orderedGroups()
642 {
643 QStringList groups;
644 std::vector<Command*> commands = Application::Instance->commandManager().getAllCommands();
645 for (const auto& command : commands) {
646 QString groupName(QString::fromLatin1(command->getGroupName()));
647 if (!groups.contains(groupName)) {
648 groups << groupName;
649 }
650 }
651 // how to sort?
652 groups.sort();
653 return groups;
654 }
655
656 ///////////////////////////////////////////////////////////////////////////////////////
657
658 PrintModel::PrintModel(QObject* parent, ButtonModel* buttonModelIn, CommandModel* commandModelIn)
659 : QAbstractTableModel(parent)
660 {
661 // NOLINTBEGIN
662 buttonModel = buttonModelIn;
663 commandModel = commandModelIn;
664 // NOLINTEND
665 }
666
667 int PrintModel::rowCount(const QModelIndex& parent) const
668 {
669 Q_UNUSED(parent);
670 return buttonModel->rowCount();
671 }
672
673 int PrintModel::columnCount(const QModelIndex& parent) const
674 {
675 Q_UNUSED(parent);
676 return 2;
677 }
678
679 QVariant PrintModel::data(const QModelIndex& index, int role) const
680 {
681 if (index.column() == 0) {
682 // button column;
683 return buttonModel->data(buttonModel->index(index.row(), 0), role);
684 }
685
686 if (index.column() == 1) {
687 // command column;
688 QString commandName(
689 buttonModel->data(buttonModel->index(index.row(), 0), Qt::UserRole).toString()
690 );
691 if (commandName.isEmpty()) {
692 return {};
693 }
694
695 QModelIndexList indexList(commandModel->match(
696 commandModel->index(0, 0),
697 Qt::UserRole,
698 QVariant(commandName),
699 1,
700 Qt::MatchWrap | Qt::MatchRecursive
701 ));
702 if (indexList.isEmpty()) {
703 return {};
704 }
705
706 return commandModel->data(indexList.at(0), role);
707 }
708 return {};
709 }
710
711 QVariant PrintModel::headerData(int section, Qt::Orientation orientation, int role) const
712 {
713 if (role != Qt::DisplayRole || orientation != Qt::Horizontal) {
714 return {};
715 }
716 if (section == 0) {
717 return {tr("Button")};
718 }
719 if (section == 1) {
720 return {tr("Command")};
721 }
722 else {
723 return {};
724 }
725 }
726
727 ///////////////////////////////////////////////////////////////////////////////////////
728
729 DlgCustomizeSpaceball::DlgCustomizeSpaceball(QWidget* parent)
730 : CustomizeActionPage(parent)
731 , buttonView(nullptr)
732 , buttonModel(nullptr)
733 , commandView(nullptr)
734 , commandModel(nullptr)
735 , clearButton(nullptr)
736 , printReference(nullptr)
737 , devModel(nullptr)
738 {
739 this->setWindowTitle(tr("Spaceball Buttons"));
740 auto app = qobject_cast<GUIApplicationNativeEventAware*>(QApplication::instance());
741 if (!app) {
742 return;
743 }
744 if (!app->isSpaceballPresent()) {
745 this->setMessage(tr("No Spaceball present"));
746 return;
747 }
748
749 setupButtonModelView();
750 setupCommandModelView();
751 connect(
752 buttonView,
753 &ButtonView::changeCommandSelection,
754 commandView,
755 &CommandView::goChangeCommandSelection
756 );
757 connect(commandView, &CommandView::changedCommand, buttonView, &ButtonView::goChangedCommand);
758 setupLayout();
759 connect(clearButton, &QPushButton::clicked, this, &DlgCustomizeSpaceball::goClear);
760 connect(printReference, &QPushButton::clicked, this, &DlgCustomizeSpaceball::goPrint);
761 }
762
763 DlgCustomizeSpaceball::~DlgCustomizeSpaceball() = default;
764
765 void DlgCustomizeSpaceball::setMessage(const QString& message)
766 {
767 auto messageLabel = new QLabel(message, this);
768 auto layout = new QVBoxLayout();
769 auto layout2 = new QHBoxLayout();
770 layout2->addStretch();
771 layout2->addWidget(messageLabel);
772 layout2->addStretch();
773 layout->addItem(layout2);
774 this->setLayout(layout);
775 }
776
777 void DlgCustomizeSpaceball::setupButtonModelView()
778 {
779 buttonModel = new ButtonModel(this);
780 buttonView = new ButtonView(this);
781 buttonView->setModel(buttonModel);
782
783 // had to do this here as the views default selection model is not created until after construction.
784 connect(
785 buttonView->selectionModel(),
786 &QItemSelectionModel::selectionChanged,
787 buttonView,
788 &ButtonView::goSelectionChanged
789 );
790 }
791
792 void DlgCustomizeSpaceball::setupCommandModelView()
793 {
794 commandModel = new CommandModel(this);
795 commandView = new CommandView(this);
796 commandView->setModel(commandModel);
797 }
798
799 void DlgCustomizeSpaceball::setupLayout()
800 {
801 auto buttonLabel = new QLabel(tr("Buttons"), this);
802 clearButton = new QPushButton(tr("Reset"), this);
803 devModel = new QComboBox(this);
804
805 // Load the devModel(s) from the config xml file
806 devModel->addItems(getModels());
807
808 // Select the current preference or the first entry
809 QString model = QString::fromStdString(
810 App::GetApplication()
811 .GetUserParameter()
812 .GetGroup("BaseApp")
813 ->GetGroup("Spaceball")
814 ->GetASCII("Model", "")
815 );
816 if (model.length() > 0) {
817 devModel->setCurrentIndex(devModel->findText(model));
818 }
819 else {
820 devModel->setCurrentIndex(0);
821 }
822
823 auto buttonGroup = new QVBoxLayout();
824 buttonGroup->addWidget(buttonLabel);
825 buttonGroup->addWidget(buttonView);
826 auto clearLayout = new QHBoxLayout();
827 clearLayout->addWidget(devModel);
828 clearLayout->addWidget(clearButton);
829 clearLayout->addStretch();
830 buttonGroup->addLayout(clearLayout);
831
832 auto splitter = new QSplitter(this);
833 auto leftPane = new QWidget(this);
834 leftPane->setLayout(buttonGroup);
835 splitter->addWidget(leftPane);
836 splitter->addWidget(commandView);
837
838 printReference = new QPushButton(tr("Print Reference"), this);
839 auto printLayout = new QHBoxLayout();
840 printLayout->addStretch();
841 printLayout->addWidget(printReference);
842
843 auto layout = new QVBoxLayout();
844 layout->addWidget(splitter);
845 layout->addLayout(printLayout);
846
847 this->setLayout(layout);
848
849 QList<int> sizes;
850 sizes << this->size().width() * 0.40;
851 sizes << this->size().width() - sizes.at(0);
852 splitter->setSizes(sizes);
853 }
854
855 void DlgCustomizeSpaceball::goClear()
856 {
857 commandView->clearSelection();
858 commandView->collapseAll();
859 commandView->setDisabled(true);
860 // buttonModel->goClear();
861
862 QByteArray currentDevice = devModel->currentText().toLocal8Bit();
863 App::GetApplication()
864 .GetUserParameter()
865 .GetGroup("BaseApp")
866 ->GetGroup("Spaceball")
867 ->SetASCII("Model", currentDevice.data());
868 buttonModel->loadConfig(currentDevice.data());
869 }
870
871 void DlgCustomizeSpaceball::goPrint()
872 {
873 auto view = new QTableView(this);
874 auto model = new PrintModel(this, buttonModel, commandModel);
875 view->horizontalHeader()->setSectionResizeMode(QHeaderView::Fixed);
876 view->setModel(model);
877 view->horizontalHeader()->resizeSection(0, 150);
878 view->horizontalHeader()->resizeSection(1, 300);
879 view->resize(600, 600);
880
881 QPrinter printer;
882 QPrintDialog printDialog(&printer, this);
883 if (printDialog.exec() == QDialog::Accepted) {
884 QPainter p(&printer);
885 view->render(&p);
886 }
887 }
888
889 bool DlgCustomizeSpaceball::event(QEvent* event)
890 {
891 if (event->type() != Spaceball::ButtonEvent::ButtonEventType) {
892 return CustomizeActionPage::event(event);
893 }
894 auto buttonEvent = dynamic_cast<Spaceball::ButtonEvent*>(event);
895 if (!buttonEvent) {
896 return true;
897 }
898 buttonEvent->setHandled(true);
899 if (buttonEvent->buttonStatus() == Spaceball::BUTTON_PRESSED) {
900 buttonModel->goButtonPress(buttonEvent->buttonNumber());
901 }
902 buttonView->selectButton(buttonEvent->buttonNumber());
903
904 return true;
905 }
906
907 void DlgCustomizeSpaceball::hideEvent(QHideEvent* event)
908 {
909 // having a crash with the last item of the macro command list
910 // being selected and that macro is removed from the macro tab. Hopefully
911 // clearing the selection will cure the problem.
912 if (buttonView) {
913 buttonView->selectionModel()->clear();
914 }
915 if (commandView) {
916 commandView->selectionModel()->clear();
917 commandView->collapseAll();
918 commandView->setEnabled(false);
919 }
920
921 CustomizeActionPage::hideEvent(event);
922 }
923
924 void DlgCustomizeSpaceball::showEvent(QShowEvent* event)
925 {
926 if (buttonView) {
927 buttonView->setFocus();
928 }
929
930 CustomizeActionPage::showEvent(event);
931 }
932
933 void DlgCustomizeSpaceball::changeEvent(QEvent* e)
934 {
935 if (e->type() == QEvent::LanguageChange) {
936 // I don't think I need do anything here. Qt should take care of it?
937 // this->setWindowTitle(tr("Spaceball"));
938 }
939 QWidget::changeEvent(e);
940 }
941
942 void DlgCustomizeSpaceball::onAddMacroAction(const QByteArray& macroName)
943 {
944 // need to get the new macro to model.
945 if (commandModel) {
946 commandModel->goAddMacro(macroName);
947 }
948 }
949
950 void DlgCustomizeSpaceball::onRemoveMacroAction(const QByteArray& macroName)
951 {
952 // need to remove macro from model.
953 if (commandModel) {
954 commandModel->goRemoveMacro(macroName);
955 }
956 // need to change any button mapped to macro to an empty string.
957 if (buttonModel) {
958 buttonModel->goMacroRemoved(macroName);
959 }
960 }
961
962 void DlgCustomizeSpaceball::onModifyMacroAction(const QByteArray& macroName)
963 {
964 // don't think I need to do anything here.
965 Q_UNUSED(macroName);
966 }
967
968 QStringList DlgCustomizeSpaceball::getModels()
969 {
970 QStringList modelList;
971 try {
972 boost::property_tree::ptree tree;
973 boost::property_tree::ptree DeviceTree;
974
975 // exception thrown if no file found
976 std::string path = App::Application::getResourceDir();
977 path += "3Dconnexion/3DConnexion.xml";
978 read_xml(path.c_str(), tree);
979
980 BOOST_FOREACH (const boost::property_tree::ptree::value_type& ButtonMap, tree.get_child("")) {
981 if ("ButtonMap" == ButtonMap.first) {
982 // Inspect ButtonMap attributes for DeviceName
983 BOOST_FOREACH (
984 const boost::property_tree::ptree::value_type& kv,
985 ButtonMap.second.get_child("<xmlattr>")
986 ) {
987 std::string Attribute;
988 std::string Value;
989
990 Attribute = kv.first.data();
991 Value = kv.second.data();
992
993 if (0 == Attribute.compare("DeviceName")) {
994 modelList << QString::fromStdString(Value);
995 }
996 }
997 }
998 }
999 }
1000 catch (const std::exception& e) {
1001 // We don't mind not finding the file to be opened
1002 Base::Console().warning("%s\n", e.what());
1003 }
1004
1005 return modelList;
1006 }
1007
1008 #include "moc_DlgCustomizeSpaceball.cpp"