Introduce main window affinity
By default a dock widget can dock into any main window. With affinities, we can now have a dock widget "belong" to a main window and only be able to dock into it (or into other floating dock widgets with the same affinity). See DockWidgetBase::setAffinity() and MainWindowBase::setAffinity().
This commit is contained in:
@@ -49,8 +49,9 @@ static MyWidget *newMyWidget()
|
||||
}
|
||||
}
|
||||
|
||||
MyMainWindow::MyMainWindow(KDDockWidgets::MainWindowOptions options, QWidget *parent)
|
||||
: MainWindow(QStringLiteral("MyMainWindow"), options, parent)
|
||||
MyMainWindow::MyMainWindow(const QString &uniqueName, KDDockWidgets::MainWindowOptions options,
|
||||
const QString &affinityName, QWidget *parent)
|
||||
: MainWindow(uniqueName, options, parent)
|
||||
{
|
||||
// qApp->installEventFilter(this);
|
||||
|
||||
@@ -88,6 +89,7 @@ MyMainWindow::MyMainWindow(KDDockWidgets::MainWindowOptions options, QWidget *pa
|
||||
saver.restoreFromDisk();
|
||||
});
|
||||
|
||||
setAffinityName(affinityName);
|
||||
createDockWidgets();
|
||||
}
|
||||
|
||||
@@ -124,6 +126,7 @@ KDDockWidgets::DockWidgetBase *MyMainWindow::newDockWidget()
|
||||
{
|
||||
static int count = 0;
|
||||
auto dock = new KDDockWidgets::DockWidget(QStringLiteral("DockWidget #%1").arg(count));
|
||||
dock->setAffinityName(affinityName()); // optional, just to show the feature. Pass -mi to the example to see incompatible dock widgets
|
||||
|
||||
if (count == 1)
|
||||
dock->setIcon(QIcon::fromTheme(QStringLiteral("mail-message")));
|
||||
|
||||
@@ -26,7 +26,9 @@ class MyMainWindow : public KDDockWidgets::MainWindow
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit MyMainWindow(KDDockWidgets::MainWindowOptions options, QWidget *parent = nullptr);
|
||||
explicit MyMainWindow(const QString &uniqueName, KDDockWidgets::MainWindowOptions options,
|
||||
const QString &affinityName = {}, // Usually not needed. Just here to show the feature.
|
||||
QWidget *parent = nullptr);
|
||||
|
||||
private:
|
||||
void createDockWidgets();
|
||||
|
||||
@@ -58,6 +58,12 @@ int main(int argc, char **argv)
|
||||
QCommandLineOption lazyResizeOption("l", QCoreApplication::translate("main", "Use lazy resize"));
|
||||
parser.addOption(lazyResizeOption);
|
||||
|
||||
QCommandLineOption multipleMainWindows("m", QCoreApplication::translate("main", "Shows two multiple main windows"));
|
||||
parser.addOption(multipleMainWindows);
|
||||
|
||||
QCommandLineOption incompatibleMainWindows("i", QCoreApplication::translate("main", "Only usable with -m. Make the two main windows incompatible with each other. (Illustrates (MainWindowBase::setAffinityName))"));
|
||||
parser.addOption(incompatibleMainWindows);
|
||||
|
||||
#if defined(DOCKS_DEVELOPER_MODE)
|
||||
QCommandLineOption noCentralFrame("c", QCoreApplication::translate("main", "No central frame"));
|
||||
parser.addOption(noCentralFrame);
|
||||
@@ -88,11 +94,33 @@ int main(int argc, char **argv)
|
||||
if (parser.isSet(lazyResizeOption))
|
||||
flags |= KDDockWidgets::Config::Flag_LazyResize;
|
||||
|
||||
if (parser.isSet(incompatibleMainWindows) && !parser.isSet(multipleMainWindows)) {
|
||||
qWarning() << "Error: Argument -i requires -m";
|
||||
return 1;
|
||||
}
|
||||
|
||||
KDDockWidgets::Config::self().setFlags(flags);
|
||||
|
||||
MyMainWindow mainWindow(options);
|
||||
MyMainWindow mainWindow(QStringLiteral("MyMainWindow"), options);
|
||||
mainWindow.setWindowTitle("Main Window 1");
|
||||
mainWindow.resize(1200, 1200);
|
||||
mainWindow.show();
|
||||
|
||||
if (parser.isSet(multipleMainWindows)) {
|
||||
// By default a dock widget can dock into any main window.
|
||||
// By setting an affinity name we can prevent that. Dock widgets of different affinities are incompatible.
|
||||
const QString affinity = parser.isSet(incompatibleMainWindows) ? QStringLiteral("affinity1")
|
||||
: QString();
|
||||
|
||||
auto mainWindow2 = new MyMainWindow(QStringLiteral("MyMainWindow-2"), options, affinity);
|
||||
if (affinity.isEmpty())
|
||||
mainWindow2->setWindowTitle("Main Window 2");
|
||||
else
|
||||
mainWindow2->setWindowTitle("Main Window 2 (different affinity)");
|
||||
|
||||
mainWindow2->resize(1200, 1200);
|
||||
mainWindow2->show();
|
||||
}
|
||||
|
||||
return app.exec();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user