Developer Documentation

DeserializeScreenshotMetadataPlugin.cc
1 /*===========================================================================*\
2 * *
3 * OpenFlipper *
4 * Copyright (c) 2001-2015, RWTH-Aachen University *
5 * Department of Computer Graphics and Multimedia *
6 * All rights reserved. *
7 * www.openflipper.org *
8 * *
9 *---------------------------------------------------------------------------*
10 * This file is part of OpenFlipper. *
11 *---------------------------------------------------------------------------*
12 * *
13 * Redistribution and use in source and binary forms, with or without *
14 * modification, are permitted provided that the following conditions *
15 * are met: *
16 * *
17 * 1. Redistributions of source code must retain the above copyright notice, *
18 * this list of conditions and the following disclaimer. *
19 * *
20 * 2. Redistributions in binary form must reproduce the above copyright *
21 * notice, this list of conditions and the following disclaimer in the *
22 * documentation and/or other materials provided with the distribution. *
23 * *
24 * 3. Neither the name of the copyright holder nor the names of its *
25 * contributors may be used to endorse or promote products derived from *
26 * this software without specific prior written permission. *
27 * *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39 * *
40 \*===========================================================================*/
41
42
43 //=============================================================================
44 //
45 // CLASS DeserializeScreenshotMetadataPlugin - IMPLEMENTATION
46 //
47 //=============================================================================
48
49 //== INCLUDES =================================================================
50
51 #include "DeserializeScreenshotMetadataPlugin.hh"
52
53 #include <OpenFlipper/BasePlugin/PluginFunctions.hh>
54 #include <OpenFlipper/BasePlugin/RPCWrappers.hh>
55 #include <OpenFlipper/common/GlobalOptions.hh>
56 #include <ACG/Scenegraph/MaterialNode.hh>
57 #include <QMessageBox>
58
59 //== IMPLEMENTATION ==========================================================
60
61
62 DeserializeScreenshotMetadataPlugin::DeserializeScreenshotMetadataPlugin() :
63 toolbar(0),
64 restoreAction(0),
65 restore_from_screenshot_dlg(0) {
66 }
67
68 void DeserializeScreenshotMetadataPlugin::initializePlugin() {
69 }
70
71 void DeserializeScreenshotMetadataPlugin::pluginsInitialized() {
72 toolbar = new QToolBar(tr("Restore from Screenshot"));
73
74 restoreAction = new QAction(tr("Restore Scene From Screenshot"), toolbar);
75 restoreAction->setIcon(
76 QIcon(OpenFlipper::Options::iconDirStr() +
77 OpenFlipper::Options::dirSeparator() +
78 "restore_from_screenshot.png"));
79
80 toolbar->addAction(restoreAction);
81
82 emit addToolbar(toolbar);
83
84 connect(restoreAction, SIGNAL(triggered(bool)), this,
85 SLOT(slot_restore_from_screenshot()));
86 }
87
88 void DeserializeScreenshotMetadataPlugin::slot_do_restore_from_screenshot() {
89 if (!restore_from_screenshot_dlg) return;
90
91 const QString restoreFileName =
92 restore_from_screenshot_dlg->getRestoreFileName();
93
94 QImage img(restoreFileName);
95 if (img.isNull()) {
96 QMessageBox::warning(0, tr("Unable to load image"),
97 tr("Unable to load image. "
98 "Unrecognized format or file not existent."),
99 QMessageBox::Ok);
100 return;
101 }
102
103 /*
104 * Deserialize View
105 */
106 if (restore_from_screenshot_dlg->restore_viewer_cb->isChecked()) {
107 QString view = img.text("View");
108 if (!view.isEmpty()) {
109 RPC::callFunction("core", "setViewAndWindowGeometry", view);
110 }
111 }
112
113 /*
114 * Deserialize Materials
115 */
116 if (restore_from_screenshot_dlg->restore_materials_cb->isChecked()) {
117 QString materials_json =
118 QString::fromUtf8("{") + img.text("Mesh Materials") +
119 QString::fromUtf8("}");
120 QVariantMap materials = ACG::json_to_variant_map(materials_json);
121
122 QMap<QString, ACG::SceneGraph::Material*> objname_to_material;
123 for (PluginFunctions::ObjectIterator
124 o_it(PluginFunctions::ALL_OBJECTS, DATA_ALL);
125 o_it != PluginFunctions::objectsEnd(); ++o_it) {
126
127 if (!o_it->materialNode()) continue;
128
129 objname_to_material[o_it->name()] = &o_it->materialNode()->material();
130 }
131
132 for (QVariantMap::const_iterator it = materials.begin();
133 it != materials.end(); ++it) {
134
135 if (objname_to_material.contains(it.key())) {
136 objname_to_material[it.key()]->deserializeFromVariantMap(
137 it.value().toMap());
138 }
139 }
140 }
141
142 /*
143 * Notify core.
144 */
145 if (restore_from_screenshot_dlg->restore_objectmd_cb->isChecked()) {
146 QStringList textKeys = img.textKeys();
147 QVector<QPair<QString, QString> > metadata;
148 metadata.reserve(textKeys.size());
149 for (QStringList::iterator it = textKeys.begin();
150 it != textKeys.end(); ++it) {
151 metadata.push_back(QPair<QString, QString>(*it, img.text(*it)));
152 }
153 emit metadataDeserialized(metadata);
154 }
155
156 }
157
158 void DeserializeScreenshotMetadataPlugin::slot_restore_from_screenshot() {
159 if (!restore_from_screenshot_dlg) {
160 restore_from_screenshot_dlg = new RestoreFromScreenshotDlg();
161 connect(restore_from_screenshot_dlg->restore_pb,
162 SIGNAL(clicked()),
163 this,
164 SLOT(slot_do_restore_from_screenshot()));
165 }
166 restore_from_screenshot_dlg->show();
167 restore_from_screenshot_dlg->raise();
168 }
169
170 void DeserializeScreenshotMetadataPlugin::slotGenericMetadataDeserialized(
171 QString key, QString value) {
172
173 }
174
175 void DeserializeScreenshotMetadataPlugin::slotObjectMetadataDeserialized(
176 QString object_name, QString value) {
177
178 }
179
180 void DeserializeScreenshotMetadataPlugin::slotObjectMetadataDeserializedJson(
181 QString object_name, QJsonDocument value) {
182
183 }
184
185 //-----------------------------------------------------------------------------
186
187
RestoreFromScreenshotDlg
Definition: RestoreFromScreenshotDlg.hh:54
RPC::callFunction
QScriptValue callFunction(QString _plugin, QString _functionName, std::vector< QScriptValue > _parameters)
Call a function provided by a plugin getting multiple parameters.
Definition: RPCWrappers.cc:55
GlobalOptions.hh
PluginFunctions::ALL_OBJECTS
const QStringList ALL_OBJECTS
Iterable object range.
Definition: PluginFunctions.hh:430
PluginFunctions.hh
PluginFunctions::objectsEnd
DLLEXPORT ObjectIterator objectsEnd()
Return Iterator to Object End.
Definition: PluginFunctionsIterator.cc:177
RPCWrappers.hh
PluginFunctions::ObjectIterator
Core Data Iterator.
Definition: PluginFunctions.hh:441
DATA_ALL
const DataType DATA_ALL(UINT_MAX)
Identifier for all available objects.