KDDockWidgets
KDDockWidgets copied to clipboard
Apply layout of one set of dockwidgets to a different set
I have the following use case, for which I couldn't find a nice solution.
My starting situation is as follows, I have an arbitrary number of MainWindow
s (MW_0
, MW_1
, ...). For each of those MainWindow
s, there is a fixed number of DockWidget
s, say for example two, which all have an affinity to this one MainWindow
. For simplicity I use the uniqueName
of the DockWidget
s as affinity-string.
For example there could be two MainWindow
s MW_0
and MW_1
and four DockWidget
s: DW_0_A
, DW_0_B
with affinity to MW_0
and DW_1_A
, DW_1_B
with affinity to MW_1
.
Now, this is what I would like to achieve: I want to save the layout of all DockWidgets
belonging to for example MW_0
, which are DW_0_A
, DW_0_B
.
And in a second step, I would like to apply this saved layout to a different group of DockWidget
s, for example DW_1_A
, DW_1_B
.
My first idea, was to use LayoutSaver::setAffinityNames
and to change the affinities of the DockWidget
s temporarily. This means just before I want to save or restore their layout I change the affinities, just to change it back again afterwards.
For example, if I want to save the layout of DW_0_A
and DW_0_B
, I would change their affinity string from "MW_0" to "MW_x", then I would create a LayoutSaver
, set its affinity also to "MW_x", save the layout and then change the affinities of DW_0_A
and DW_0_B
back to "MW_0" again.
This doesn't work, because one cannot change the affinities of a DockWidget
.
void DockWidget::setAffinities(..)
...
if (!d->affinities.isEmpty()) {
KDDW_ERROR("Affinity is already set, refusing to change."
"Submit a feature request with a good justification.");
return;
}
...
I don't know if this case is a good justification to allow this, you tell me ;) But in my opinion, it would be kind of a hacky solution with this changing of affinities back and forth.
I found a different workaround, which is even more ugly: I modify the uniqueNames
within the serialized layout. In combination with LayoutSaver::setAffinityNames
, I can "transfer" the layout of one set of DockWidget
s to another.