/
SampleView.html
657 lines (459 loc) · 28.6 KB
/
SampleView.html
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
<!DOCTYPE html>
<html>
<head>
<title>
SampleView Plugin
</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<link href="../../../3rdparty/googleCodePrettify/prettify.css" rel="stylesheet" type="text/css"/>
<link href="../../res/stylesheet.css" rel="stylesheet" type="text/css"/>
<script src="../../../3rdparty/googleCodePrettify/prettify.js" type="text/javascript"></script>
<script src="../../../3rdparty/jQuery/jquery.js" type="text/javascript"></script>
<script src="../../../res/common.js" type="text/javascript"></script>
<script src="../../res/menu.js" type="text/javascript"></script>
</head>
<body onload="prettyPrint();" ondragstart="return false;" ondrop="return false;">
<script type="text/javascript">
headerAndContentsMenu("SampleView Plugin", "../../..");
</script>
<p>
The purpose of this plugin is to provide a view that gives some information about the current file.
</p>
<div class="section">
File structure
</div>
<pre><a href="https://github.com/opencor/opencor/tree/master/src/plugins/sample/SampleView/i18n">i18n</a>
└─ <a href="https://github.com/opencor/opencor/tree/master/src/plugins/sample/SampleView/i18n/SampleView_fr.ts">SampleView_fr.ts</a>
<a href="https://github.com/opencor/opencor/tree/master/src/plugins/sample/SampleView/res">res</a>
└─ <a href="https://github.com/opencor/opencor/tree/master/src/plugins/sample/SampleView/res/SampleView_i18n.qrc.in">SampleView_i18n.qrc.in</a>
<a href="https://github.com/opencor/opencor/tree/master/src/plugins/sample/SampleView/src">src</a>
├─ <a href="https://github.com/opencor/opencor/tree/master/src/plugins/sample/SampleView/src/sampleviewplugin.cpp">sampleviewplugin.cpp</a>
├─ <a href="https://github.com/opencor/opencor/tree/master/src/plugins/sample/SampleView/src/sampleviewplugin.h">sampleviewplugin.h</a>
├─ <a href="https://github.com/opencor/opencor/tree/master/src/plugins/sample/SampleView/src/sampleviewplugin.json">sampleviewplugin.json</a>
├─ <a href="https://github.com/opencor/opencor/tree/master/src/plugins/sample/SampleView/src/sampleviewwidget.cpp">sampleviewwidget.cpp</a>
├─ <a href="https://github.com/opencor/opencor/tree/master/src/plugins/sample/SampleView/src/sampleviewwidget.h">sampleviewwidget.h</a>
└─ <a href="https://github.com/opencor/opencor/tree/master/src/plugins/sample/SampleView/src/sampleviewwidget.ui">sampleviewwidget.ui</a>
<a href="https://github.com/opencor/opencor/tree/master/src/plugins/sample/SampleView/CMakeLists.txt">CMakeLists.txt</a></pre>
<div class="section">
Category
</div>
<p>
Our plugin is part of the <a href="https://github.com/opencor/opencor/tree/master/src/plugins/sample/">Sample</a> category, which means that its code can be found under <a href="https://github.com/opencor/opencor/blob/master/src/plugins/sample/SampleView/"><code>[OpenCOR]/src/plugins/sample/SampleView/</code></a>.
</p>
<div class="section">
Interfaces
</div>
<p>
We want our plugin to interact with OpenCOR. This means that it needs to implement some interfaces (click <a href="index.html#Interfaces">here</a> for some information on interfaces).
</p>
<p>
More specifically, we want our plugin to be a view, so we need to implement both the <a href="https://github.com/opencor/opencor/blob/master/src/plugins/filehandlinginterface.inl">File handling</a>, <a href="https://github.com/opencor/opencor/blob/master/src/plugins/plugininterface.inl">Plugin</a> and <a href="https://github.com/opencor/opencor/blob/master/src/plugins/viewinterface.inl">View</a> interfaces. While we are at it, we might as well internationalise our plugin, in which case it means that we also need to implement the <a href="https://github.com/opencor/opencor/blob/master/src/plugins/i18ninterface.inl">Internationalisation</a> interface.
</p>
<div class="section">
CMake project
</div>
<p>
As for any OpenCOR plugin, our plugin has a <a href="https://github.com/opencor/opencor/blob/master/src/plugins/sample/SampleView/CMakeLists.txt"><code>CMakeLists.txt</code></a> file, which contents is:
</p>
<pre class="prettyprint linenums">PROJECT(SampleViewPlugin)
# Add the plugin
ADD_PLUGIN(SampleView
SOURCES
<a href="https://github.com/opencor/opencor/blob/master/src/plugins/filehandlinginterface.cpp"><code>../../filehandlinginterface.cpp</code></a>
<a href="https://github.com/opencor/opencor/blob/master/src/plugins/i18ninterface.cpp"><code>../../i18ninterface.cpp</code></a>
<a href="https://github.com/opencor/opencor/blob/master/src/plugins/plugininfo.cpp"><code>../../plugininfo.cpp</code></a>
<a href="https://github.com/opencor/opencor/blob/master/src/plugins/plugininterface.cpp"><code>../../plugininterface.cpp</code></a>
<a href="https://github.com/opencor/opencor/blob/master/src/plugins/viewinterface.cpp"><code>../../viewinterface.cpp</code></a>
<a href="https://github.com/opencor/opencor/blob/master/src/plugins/sample/SampleView/src/sampleviewplugin.cpp"><code>src/sampleviewplugin.cpp</code></a>
<a href="https://github.com/opencor/opencor/blob/master/src/plugins/sample/SampleView/src/sampleviewwidget.cpp"><code>src/sampleviewwidget.cpp</code></a>
HEADERS_MOC
<a href="https://github.com/opencor/opencor/blob/master/src/plugins/sample/SampleView/src/sampleviewplugin.h"><code>src/sampleviewplugin.h</code></a>
<a href="https://github.com/opencor/opencor/blob/master/src/plugins/sample/SampleView/src/sampleviewwidget.h"><code>src/sampleviewwidget.h</code></a>
UIS
<a href="https://github.com/opencor/opencor/blob/master/src/plugins/sample/SampleView/src/sampleviewwidget.ui"><code>src/sampleviewwidget.ui</code></a>
PLUGINS
Core
)</pre>
<p>
Some of the interfaces our plugin implements come with a <code>.cpp</code> file, so we reference them (lines 7, 8, 10 and 11). Then, our plugin needs the <a href="https://github.com/opencor/opencor/tree/master/src/plugins/miscellaneous/Core/">Core</a> plugin, so it is referenced (line 21) using the <code>PLUGINS</code> keyword (line 20). Our plugin provides OpenCOR with a test view, which is implemented using various files (lines 14, 17 and 19). One of those files is a <code>.ui</code> file, which is referenced using the <code>UIS</code> keyword (line 18).
</p>
<div class="section">
Plugin information
</div>
<p>
Our <a href="index.html#Plugin information">plugin information</a> can be found in <a href="https://github.com/opencor/opencor/blob/master/src/plugins/sample/SampleView/src/sampleviewplugin.cpp"><code>sampleviewplugin.cpp</code></a>, <a href="https://github.com/opencor/opencor/blob/master/src/plugins/sample/SampleView/src/sampleviewplugin.h"><code>sampleviewplugin.h</code></a> and <a href="https://github.com/opencor/opencor/blob/master/src/plugins/sample/SampleView/src/sampleviewplugin.json"><code>sampleviewplugin.json</code></a>. Starting with <a href="https://github.com/opencor/opencor/blob/master/src/plugins/sample/SampleView/src/sampleviewplugin.h"><code>sampleviewplugin.h</code></a>, its contents is:
</p>
<pre class="prettyprint linenums:28">#include "<a href="https://github.com/opencor/opencor/blob/master/src/plugins/filehandlinginterface.h"><code>filehandlinginterface.h</code></a>"
#include "<a href="https://github.com/opencor/opencor/blob/master/src/plugins/i18ninterface.h"><code>i18ninterface.h</code></a>"
#include "<a href="https://github.com/opencor/opencor/blob/master/src/plugins/plugininfo.h"><code>plugininfo.h</code></a>"
#include "<a href="https://github.com/opencor/opencor/blob/master/src/plugins/plugininterface.h"><code>plugininterface.h</code></a>"
#include "<a href="https://github.com/opencor/opencor/blob/master/src/plugins/viewinterface.h"><code>viewinterface.h</code></a>"
//==============================================================================
namespace OpenCOR {
namespace SampleView {
//==============================================================================
PLUGININFO_FUNC SampleViewPluginInfo();
//==============================================================================
class SampleViewWidget;
//==============================================================================
class SampleViewPlugin : public QObject, public FileHandlingInterface,
public I18nInterface, public PluginInterface,
public ViewInterface
{
Q_OBJECT
Q_PLUGIN_METADATA(IID "OpenCOR.SampleViewPlugin" FILE "<a href="https://github.com/opencor/opencor/blob/master/src/plugins/sample/SampleView/src/sampleviewplugin.json"><code>sampleviewplugin.json</code></a>")
Q_INTERFACES(OpenCOR::FileHandlingInterface)
Q_INTERFACES(OpenCOR::I18nInterface)
Q_INTERFACES(OpenCOR::PluginInterface)
Q_INTERFACES(OpenCOR::ViewInterface)
public:
explicit SampleViewPlugin();
#include "<a href="https://github.com/opencor/opencor/blob/master/src/plugins/filehandlinginterface.inl"><code>filehandlinginterface.inl</code></a>"
#include "<a href="https://github.com/opencor/opencor/blob/master/src/plugins/i18ninterface.inl"><code>i18ninterface.inl</code></a>"
#include "<a href="https://github.com/opencor/opencor/blob/master/src/plugins/plugininterface.inl"><code>plugininterface.inl</code></a>"
#include "<a href="https://github.com/opencor/opencor/blob/master/src/plugins/viewinterface.inl"><code>viewinterface.inl</code></a>"
private:
SampleViewWidget *mViewWidget;
QString mFileName;
};
//==============================================================================
} // namespace SampleView
} // namespace OpenCOR</pre>
<p>
As mentioned above, our plugin implements some interfaces, which means that their header file is included (lines 28, 29, 31 and 32). It also means that our plugin class inherits from those interfaces (lines 49-51), as well as make calls to the <code>Q_INTERFACES()</code> macro to let <a href="https://www.qt.io/">Qt</a> know which interfaces it implements (lines 57-60). Finally, we include the inline files (lines 65-68) that declare various methods that must be implemented by our plugin (see the <a href="#Interfaces implementation">next section</a>). (The rest of the class definition is specific to our plugin and is discussed <a href="#Plugin specific">below</a>.)
</p>
<p>
The C function that is used by OpenCOR to retrieve some <a href="index.html#Basic information">basic information</a> about our plugin can be found in <a href="https://github.com/opencor/opencor/blob/master/src/plugins/sample/SampleView/src/sampleviewplugin.cpp"><code>sampleviewplugin.cpp</code></a>:
</p>
<pre class="prettyprint linenums:40">PLUGININFO_FUNC SampleViewPluginInfo()
{
Descriptions descriptions;
descriptions.insert("en", QString::fromUtf8("a plugin that provides a test view."));
descriptions.insert("fr", QString::fromUtf8("une extension qui fournit une vue de test."));
return new PluginInfo(PluginInfo::Sample, true, false,
QStringList() << "Core",
descriptions);
}</pre>
<p>
As can be seen, our plugin is selectable by the user, but it does not offer direct <a href="https://en.wikipedia.org/wiki/Command-line_interface">CLI</a> support (line 47). It also has a direct dependency on the <a href="https://github.com/opencor/opencor/tree/master/src/plugins/miscellaneous/Core/">Core</a> plugin (line 48).
</p>
<span class="anchor" id="Interfaces implementation"></span>
<div class="section">
Interfaces implementation
</div>
<p>
The implementation of the interfaces' various methods can also be found in <a href="https://github.com/opencor/opencor/blob/master/src/plugins/sample/SampleView/src/sampleviewplugin.cpp"><code>sampleviewplugin.cpp</code></a>. The methods are grouped by interface and are ordered alphabetically. The interfaces are also ordered alphabetically, making it easier to read and maintain the code.
</p>
<p>
We start with the <a href="https://github.com/opencor/opencor/blob/master/src/plugins/filehandlinginterface.inl">File handling</a> interface:
</p>
<pre class="prettyprint linenums:59">//==============================================================================
// File handling interface
//==============================================================================
bool SampleViewPlugin::saveFile(const QString &pOldFileName,
const QString &pNewFileName,
bool &pNeedFeedback)
{
Q_UNUSED(pOldFileName);
Q_UNUSED(pNewFileName);
Q_UNUSED(pNeedFeedback);
// We don't handle this interface...
return false;
}
//==============================================================================
void SampleViewPlugin::fileOpened(const QString &pFileName)
{
Q_UNUSED(pFileName);
// We don't handle this interface...
}
//==============================================================================
void SampleViewPlugin::filePermissionsChanged(const QString &pFileName)
{
// The given file has had its permissions changed, so update our view
// widget, if needed
if (!pFileName.compare(mFileName))
mViewWidget->update(pFileName);
}
//==============================================================================
void SampleViewPlugin::fileModified(const QString &pFileName)
{
Q_UNUSED(pFileName);
// We don't handle this interface...
}
//==============================================================================
void SampleViewPlugin::fileReloaded(const QString &pFileName,
const bool &pFileChanged,
const bool &pFileJustSaved)
{
Q_UNUSED(pFileJustSaved);
// The given file has been reloaded, so update our view widget, if needed
if (pFileChanged && !pFileName.compare(mFileName))
mViewWidget->update(pFileName);
}
//==============================================================================
void SampleViewPlugin::fileRenamed(const QString &pOldFileName,
const QString &pNewFileName)
{
Q_UNUSED(pOldFileName);
// The given file has been renamed, so update our view widget, if needed
if (!pOldFileName.compare(mFileName)) {
mFileName = pNewFileName;
mViewWidget->update(pNewFileName);
}
}
//==============================================================================
void SampleViewPlugin::fileClosed(const QString &pFileName)
{
// The given file has been closed, so update our internals, if needed
if (!pFileName.compare(mFileName))
mFileName = QString();
}
//==============================================================================</pre>
<p>
Our plugin provides a view and, as such, should at least handle some of the <a href="https://github.com/opencor/opencor/blob/master/src/plugins/filehandlinginterface.inl">File handling</a> interface's methods. Here, we want our plugin to provide some information about the current file, so we do not need to implement <code>saveFile()</code> (lines 63-74), <code>fileOpened()</code> (lines 78-83) and <code>fileModified()</code> (lines 98-103). On the other hand, should the current file have its permissions changed or be renamed, then we want to update the information presented in our view. We do this by implementing the <code>filePermissionsChanged()</code> (lines 87-94) and <code>fileReloaded()</code> (lines 107-117) methods. The same holds true if the current file gets renamed, in which case we also want to update <code>mFileName</code> (see <code>fileRenamed()</code>; lines 121-133). Finally, we want to reset <code>mFileName</code> if the current file gets closed (see <code>fileClosed()</code>; lines 137-143).
</p>
<p>
Next, we have the <a href="https://github.com/opencor/opencor/blob/master/src/plugins/i18ninterface.inl">Internationalisation</a> interface:
</p>
<pre class="prettyprint linenums:145">//==============================================================================
// I18n interface
//==============================================================================
void SampleViewPlugin::retranslateUi()
{
// Retranslate our view widget, if needed
if (!mFileName.isEmpty())
mViewWidget->retranslateUi();
}
//==============================================================================</pre>
<p>
If some information is being shown for a file, then we ask our view to retranslate itself.
</p>
<p>
After the <a href="https://github.com/opencor/opencor/blob/master/src/plugins/i18ninterface.inl">Internationalisation</a> interface, we have the <a href="https://github.com/opencor/opencor/blob/master/src/plugins/plugininterface.inl">Plugin</a> interface:
</p>
<pre class="prettyprint linenums:157">//==============================================================================
// Plugin interface
//==============================================================================
bool SampleViewPlugin::definesPluginInterfaces()
{
// We don't handle this interface...
return false;
}
//==============================================================================
bool SampleViewPlugin::pluginInterfacesOk(const QString &pFileName,
QObject *pInstance)
{
Q_UNUSED(pFileName);
Q_UNUSED(pInstance);
// We don't handle this interface...
return false;
}
//==============================================================================
void SampleViewPlugin::initializePlugin()
{
// Create our Sample view widget
mViewWidget = new SampleViewWidget(Core::mainWindow());
// Hide our Sample view widget since it may not initially be shown in our
// central widget
mViewWidget->setVisible(false);
}
//==============================================================================
void SampleViewPlugin::finalizePlugin()
{
// We don't handle this interface...
}
//==============================================================================
void SampleViewPlugin::pluginsInitialized(const Plugins &pLoadedPlugins)
{
Q_UNUSED(pLoadedPlugins);
// We don't handle this interface...
}
//==============================================================================
void SampleViewPlugin::loadSettings(QSettings *pSettings)
{
Q_UNUSED(pSettings);
// We don't handle this interface...
}
//==============================================================================
void SampleViewPlugin::saveSettings(QSettings *pSettings) const
{
Q_UNUSED(pSettings);
// We don't handle this interface...
}
//==============================================================================
void SampleViewPlugin::handleUrl(const QUrl &pUrl)
{
Q_UNUSED(pUrl);
// We don't handle this interface...
}
//==============================================================================</pre>
<p>
The only method of interest to our plugin is <code>initializePlugin()</code> (lines 183-193), which is where we initialise <code>mViewWidget</code>, our view. All the other methods (<code>finalizePlugin()</code>, <code>pluginsInitialized()</code>, <code>loadSettings()</code>, <code>saveSettings()</code> and <code>handleUrl()</code>) are left empty.
</p>
<p>
Finally, we have the <a href="https://github.com/opencor/opencor/blob/master/src/plugins/viewinterface.inl">View</a> interface:
</p>
<pre class="prettyprint linenums:238">//==============================================================================
// View interface
//==============================================================================
ViewInterface::Mode SampleViewPlugin::viewMode() const
{
// Return our mode
return SampleMode;
}
//==============================================================================
QStringList SampleViewPlugin::viewMimeTypes() const
{
// Return the MIME types we support, i.e. any in our case
return QStringList();
}
//==============================================================================
QString SampleViewPlugin::viewMimeType(const QString &pFileName) const
{
Q_UNUSED(pFileName)
// Return the MIME type for the given file
return QString();
}
//==============================================================================
QString SampleViewPlugin::viewDefaultFileExtension() const
{
// Return the default file extension we support
return QString();
}
//==============================================================================
QWidget * SampleViewPlugin::viewWidget(const QString &pFileName)
{
// Update and return our Sample view widget using the given file
mFileName = pFileName;
mViewWidget->update(pFileName);
return mViewWidget;
}
//==============================================================================
void SampleViewPlugin::removeViewWidget(const QString &pFileName)
{
Q_UNUSED(pFileName);
// Reset our internals
mFileName = QString();
}
//==============================================================================
QString SampleViewPlugin::viewName() const
{
// Return our Sample view's name
return tr("Sample");
}
//==============================================================================
QIcon SampleViewPlugin::fileTabIcon(const QString &pFileName) const
{
Q_UNUSED(pFileName);
// We don't handle this interface...
return QIcon();
}
//==============================================================================</pre>
<p>
Our plugin provides a view, so OpenCOR needs to know about its name (see <code>viewName()</code>; lines 304-309), its type (see <code>viewMode()</code>; lines 242-247), the MIME types it supports (see <code>viewMimeTypes()</code>; lines 251-256), the MIME type supported by a given file (see <code>viewMimeType()</code>; lines 260-267), the default file extension it supports (see <code>viewDefaultFileExtension()</code>; lines 271-276) and whether it needs a special tab icon (see <code>fileTabIcon()</code>; lines 313-320). OpenCOR also needs to know the widget that is used for the view and this for a given file (see <code>viewWidget()</code>; lines 280-289). Note that our plugin uses only one view widget (and updates its contents based on the file that is currently active), but it might perfectly use one per file. Finally, our plugin needs to handle the case where a view widget is to be removed (see <code>removeViewWidget()</code>; lines 293-300), which happens whenever a file gets closed.
</p>
<span class="anchor" id="Plugin specific"></span>
<div class="section">
Plugin specific
</div>
<p>
Some extra work is needed to get our plugin to do what it is supposed to be doing, and this is done via the <code>SampleViewWidget</code> class in <a href="https://github.com/opencor/opencor/blob/master/src/plugins/sample/SampleView/src/sampleviewwidget.h"><code>sampleviewwidget.h</code></a>:
</p>
<pre class="prettyprint linenums:28">#include "<a href="https://github.com/opencor/opencor/blob/master/src/plugins/miscellaneous/Core/src/viewwidget.h"><code>viewwidget.h</code></a>"
//==============================================================================
namespace Ui {
class SampleViewWidget;
}
//==============================================================================
namespace OpenCOR {
namespace SampleView {
//==============================================================================
class SampleViewWidget : public Core::ViewWidget
{
Q_OBJECT
public:
explicit SampleViewWidget(QWidget *pParent);
~SampleViewWidget();
virtual void retranslateUi();
virtual QWidget * widget(const QString &pFileName);
void update(const QString &pFileName);
private:
Ui::SampleViewWidget *mGui;
QString mFileName;
};
//==============================================================================
} // namespace SampleView
} // namespace OpenCOR</pre>
<p>
<code>SampleViewWidget</code> inherits from <code>Core::ViewWidget</code>, which is defined in the <a href="https://github.com/opencor/opencor/tree/master/src/plugins/miscellaneous/Core/">Core</a> plugin and is an extended version of <a href="https://www.qt.io/">Qt</a>'s <code>QWidget</code> (line 43). It requires implementing the <code>widget()</code> method, which purpose is to return the exact widget that is to be shown in the view (line 53). In the present case, it is <code>SampleViewWidget</code> itself. Otherwise, <code>SampleViewWidget</code> also comes with a GUI file, which describes the layout of our plugin window (<a href="https://github.com/opencor/opencor/blob/master/src/plugins/sample/SampleView/src/sampleviewwidget.ui"><code>sampleviewwidget.ui</code></a>). The <code>update()</code> method is used by our plugin to update the contents of our view using information about the given file (line 55).
</p>
<p>
The implementation of <code>SampleViewWidget</code> can be found in <a href="https://github.com/opencor/opencor/blob/master/src/plugins/sample/SampleView/src/sampleviewwidget.cpp"><code>sampleviewwidget.cpp</code></a>:
</p>
<pre class="prettyprint linenums:24">#include "<a href="https://github.com/opencor/opencor/blob/master/src/plugins/miscellaneous/Core/src/corecliutils.h"><code>corecliutils.h</code></a>"
#include "<a href="https://github.com/opencor/opencor/blob/master/src/plugins/miscellaneous/Core/src/filemanager.h"><code>filemanager.h</code></a>"
#include "<a href="https://github.com/opencor/opencor/blob/master/src/plugins/sample/SampleView/src/sampleviewwidget.h"><code>sampleviewwidget.h</code></a>"
//==============================================================================
#include "ui_sampleviewwidget.h"
//==============================================================================
#include <QFile>
//==============================================================================
namespace OpenCOR {
namespace SampleView {
//==============================================================================
SampleViewWidget::SampleViewWidget(QWidget *pParent) :
ViewWidget(pParent),
mGui(new Ui::SampleViewWidget),
mFileName(QString())
{
// Delete the layout that comes with ViewWidget
delete layout();
// Set up the GUI
mGui->setupUi(this);
}
//==============================================================================
SampleViewWidget::~SampleViewWidget()
{
// Delete the GUI
delete mGui;
}
//==============================================================================
void SampleViewWidget::retranslateUi()
{
// Retranslate our GUI
mGui->retranslateUi(this);
// Update ourself too since some widgets will have been reset following the
// retranslation (e.g. mGui->fileNameValue)
update(mFileName);
}
//==============================================================================
QWidget * SampleViewWidget::widget(const QString &pFileName)
{
Q_UNUSED(pFileName);
// Return the requested (self) widget
return this;
}
//==============================================================================
void SampleViewWidget::update(const QString &pFileName)
{
// Keep track of the given file name
mFileName = pFileName;
// Initialise our GUI with some information about the given file
mGui->fileNameValue->setText(pFileName);
Core::FileManager *fileManagerInstance = Core::FileManager::instance();
mGui->lockedValue->setText(fileManagerInstance->isLocked(pFileName)?tr("Yes"):tr("No"));
QString sha1Value = fileManagerInstance->sha1(pFileName);
mGui->sha1Value->setText(sha1Value.isEmpty()?"???":sha1Value);
mGui->sizeValue->setText(Core::sizeAsString(QFile(pFileName).size()));
}
//==============================================================================
} // namespace SampleView
} // namespace OpenCOR</pre>
<p>
<code>retranslateUi()</code> (lines 68-78) retranslates our view, as well as return its exact widget (lines 82-89) and update its contents using <code>update()</code> (lines 93-111).
</p>
<script type="text/javascript">
copyright("../../..");
</script>
</body>
</html>