[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    [kscreen/osd] kded: kscreen/kded: Put set config into osd
From:       Leslie Zhai <xiangzhai83 () gmail ! com>
Date:       2015-07-17 1:53:41
Message-ID: E1ZFuqL-0005FF-Ma () scm ! kde ! org
[Download RAW message or body]

Git commit 5173775d511be65a56938ea6e5e6a89b3fec06b0 by Leslie Zhai.
Committed on 17/07/2015 at 01:52.
Pushed by lesliezhai into branch 'osd'.

kscreen/kded: Put set config into osd

M  +5    -127  kded/daemon.cpp
M  +1    -20   kded/daemon.h
M  +120  -7    kded/osdwidget.cpp
M  +29   -7    kded/osdwidget.h

http://commits.kde.org/kscreen/5173775d511be65a56938ea6e5e6a89b3fec06b0

diff --git a/kded/daemon.cpp b/kded/daemon.cpp
index 615ffd5..69841e5 100644
--- a/kded/daemon.cpp
+++ b/kded/daemon.cpp
@@ -1,5 +1,6 @@
 /*************************************************************************************
  *  Copyright (C) 2012 by Alejandro Fiestas Olivares <afiestas@kde.org>              *
+ *  Copyright (C) 2015 Leslie Zhai <xiang.zhai@i-soft.com.cn>                        *
  *                                                                                   *
  *  This program is free software; you can redistribute it and/or                    *
  *  modify it under the terms of the GNU General Public License                      *
@@ -32,7 +33,6 @@
 #include <KActionCollection>
 #include <KPluginFactory>
 #include <KGlobalAccel>
-#include <KToolInvocation>
 
 #include <kscreen/config.h>
 #include <kscreen/output.h>
@@ -42,8 +42,6 @@
 
 K_PLUGIN_FACTORY(KScreenDaemonFactory, registerPlugin<KScreenDaemon>();)
 
-static const QString lvdsPrefix = "LVDS";
-
 KScreenDaemon::KScreenDaemon(QObject* parent, const QList< QVariant >& )
  : KDEDModule(parent)
  , m_monitoredConfig(0)
@@ -53,7 +51,7 @@ KScreenDaemon::KScreenDaemon(QObject* parent, const QList< QVariant >& )
  , m_buttonTimer(new QTimer())
  , m_saveTimer(new QTimer())
  , m_lidClosedTimer(new QTimer())
- , m_osdWidget(new OsdWidget)
+ , m_osdWidget(nullptr)
  
 {
     QMetaObject::invokeMethod(this, "requestConfig", Qt::QueuedConnection);
@@ -137,15 +135,10 @@ void KScreenDaemon::init()
     connect(Generator::self(), &Generator::ready,
             this, &KScreenDaemon::applyConfig);
 
-    connect(m_osdWidget, &OsdWidget::pcScreenOnly, 
-            this, &KScreenDaemon::slotPcScreenOnly);
-    connect(m_osdWidget, &OsdWidget::mirror, this, &KScreenDaemon::slotMirror);
-    connect(m_osdWidget, &OsdWidget::extend, this, &KScreenDaemon::slotExtend);
-    connect(m_osdWidget, &OsdWidget::secondScreenOnly, 
-            this, &KScreenDaemon::slotSecondScreenOnly);
-
     Generator::self()->setCurrentConfig(m_monitoredConfig);
     monitorConnectedChange();
+
+    m_osdWidget = new OsdWidget(m_monitoredConfig);
 }
 
 void KScreenDaemon::doApplyConfig(const KScreen::ConfigPtr& config)
@@ -323,18 +316,7 @@ void KScreenDaemon::outputConnectedChanged()
         }
     }
 
-    unsigned int outputConnected = 0;
-    bool hasPrimary = false;
-    for (KScreen::OutputPtr &output : m_monitoredConfig->outputs()) {
-        if (output->isPrimary() || output->name().contains(lvdsPrefix))
-            hasPrimary = true;
-
-        if (output->isConnected())
-            outputConnected++;
-    }
-
-    if (hasPrimary && outputConnected > 1)
-        m_osdWidget->showAll();
+    m_osdWidget->showAll();
 }
 
 
@@ -388,96 +370,6 @@ void KScreenDaemon::disableOutput(KScreen::ConfigPtr &config, KScreen::OutputPtr
     output->setEnabled(false);
 }
 
-void KScreenDaemon::slotPcScreenOnly() 
-{
-    SetConfigOpThread *thread = nullptr;
-    
-    for (KScreen::OutputPtr &output : m_monitoredConfig->outputs()) {
-        // if there is NO primary set, isPrimary is unreliable!
-        if (output->isPrimary() || output->name().contains(lvdsPrefix))
-            output->setEnabled(true);
-        else
-            output->setEnabled(false);
-    }
-
-    thread = new SetConfigOpThread(m_monitoredConfig);
-    thread->start();
-}
-
-void KScreenDaemon::slotMirror() 
-{
-    KScreen::OutputPtr primaryOutput;
-    QPoint primaryPos(0, 0);
-    SetConfigOpThread *thread = nullptr;
-
-    // TODO: it needs to find the same resoluation, if there is none?
-    for (KScreen::OutputPtr &output : m_monitoredConfig->outputs()) {
-        // setEnabled does not work, so just use xrandr, for example, 
-        // xrandr --output LVDS1 --auto
-        KToolInvocation::kdeinitExec(QString("xrandr"), QStringList() << 
-            QString("--output") << output->name() << QString("--auto"));
-
-        if (output->isPrimary() || output->name().contains(lvdsPrefix)) {
-            primaryOutput = output;
-            primaryPos = output->pos();
-        } else {
-            output->setPos(primaryPos);
-        }
-    }
-
-    thread = new SetConfigOpThread(m_monitoredConfig);
-    thread->start();
-}
-
-void KScreenDaemon::slotExtend() 
-{
-    QPoint secondPos;
-    SetConfigOpThread *thread = nullptr;
-
-    for (KScreen::OutputPtr &output : m_monitoredConfig->outputs()) {
-        KToolInvocation::kdeinitExec(QString("xrandr"), QStringList() <<
-            QString("--output") << output->name() << QString("--auto"));
-
-        if (output->isPrimary() || output->name().contains(lvdsPrefix)) {
-            secondPos = output->pos();
-            secondPos.setX(output->pos().x() + output->size().width());
-        } else {
-            output->setPos(secondPos);
-        }
-    }
-
-    thread = new SetConfigOpThread(m_monitoredConfig);
-    thread->start();
-}
-
-void KScreenDaemon::slotSecondScreenOnly() 
-{
-    SetConfigOpThread *thread = nullptr;
-    QString primaryName;
-    QString secondName;
-    
-    for (KScreen::OutputPtr &output : m_monitoredConfig->outputs()) {
-        if (output->isPrimary() || output->name().contains(lvdsPrefix)) {
-            output->setEnabled(false);
-            primaryName = output->name();
-        } else {
-            output->setEnabled(true);
-            secondName = output->name();
-            break;
-        }
-    }
-
-    // fail to secondScreenOnly
-    //thread = new SetConfigOpThread(m_monitoredConfig);
-    //thread->start();
-
-    // so just use xrandr command, for example, 
-    // xrandr --output LVDS1 --off --output VGA1 --auto
-    KToolInvocation::kdeinitExec(QString("xrandr"), 
-        QStringList() << QString("--output") << primaryName << QString("--off") 
-        << QString("--output") << secondName << QString("--auto"));
-}
-
 KScreen::OutputPtr KScreenDaemon::findEmbeddedOutput(const KScreen::ConfigPtr &config)
 {
     Q_FOREACH (const KScreen::OutputPtr &output, config->outputs()) {
@@ -489,18 +381,4 @@ KScreen::OutputPtr KScreenDaemon::findEmbeddedOutput(const KScreen::ConfigPtr &c
     return KScreen::OutputPtr();
 }
 
-SetConfigOpThread::SetConfigOpThread(KScreen::ConfigPtr config) 
-  : m_config(config) 
-{
-}
-
-void SetConfigOpThread::run() 
-{
-    if (!KScreen::Config::canBeApplied(m_config))
-        return;
-
-    auto *op = new KScreen::SetConfigOperation(m_config);
-    op->exec();
-}
-
 #include "daemon.moc"
diff --git a/kded/daemon.h b/kded/daemon.h
index 4fcf19a..ef13665 100644
--- a/kded/daemon.h
+++ b/kded/daemon.h
@@ -1,5 +1,6 @@
 /*************************************************************************************
 *  Copyright (C) 2012 by Alejandro Fiestas Olivares <afiestas@kde.org>              *
+*  Copyright (C) 2015 Leslie Zhai <xiang.zhai@i-soft.com.cn>                        *
 *                                                                                   *
 *  This program is free software; you can redistribute it and/or                    *
 *  modify it under the terms of the GNU General Public License                      *
@@ -20,7 +21,6 @@
 #define KSCREN_DAEMON_H
 
 #include <QVariant>
-#include <QThread>
 
 #include <kdedmodule.h>
 
@@ -63,11 +63,6 @@ class Q_DECL_EXPORT KScreenDaemon : public KDEDModule
         void setMonitorForChanges(bool enabled);
         void outputConnectedChanged();
 
-        void slotPcScreenOnly();
-        void slotMirror();
-        void slotExtend();
-        void slotSecondScreenOnly();
-
     Q_SIGNALS:
         void outputConnected(const QString &outputName);
         void unknownOutputConnected(const QString &outputName);
@@ -89,18 +84,4 @@ class Q_DECL_EXPORT KScreenDaemon : public KDEDModule
         OsdWidget* m_osdWidget;
 };
 
-class SetConfigOpThread : public QThread 
-{
-    Q_OBJECT
-
-public:
-    explicit SetConfigOpThread(KScreen::ConfigPtr);
-
-protected:
-    void run();
-
-private:
-    KScreen::ConfigPtr m_config;
-};
-
 #endif /*KSCREN_DAEMON_H*/
diff --git a/kded/osdwidget.cpp b/kded/osdwidget.cpp
index 83fb3b1..bf40ea2 100644
--- a/kded/osdwidget.cpp
+++ b/kded/osdwidget.cpp
@@ -24,14 +24,21 @@
 #include <QListWidget>
 
 #include <KLocalizedString>
+#include <KToolInvocation>
+
+#include <kscreen/output.h>
+#include <kscreen/setconfigoperation.h>
 
 static const QString PC_SCREEN_ONLY_MODE = i18n("PC screen only");
 static const QString MIRROR_MODE = i18n("Mirror");
 static const QString EXTEND_MODE = i18n("Extend");
 static const QString SECOND_SCREEN_ONLY_MODE = i18n("Second screen only");
 
-OsdWidget::OsdWidget(QWidget *parent, Qt::WindowFlags f) 
-  : QWidget(parent, f) 
+OsdWidget::OsdWidget(KScreen::ConfigPtr config, 
+                     QWidget *parent, 
+                     Qt::WindowFlags f) 
+  : QWidget(parent, f),
+    m_config(config)
 {
     setFixedSize(467, 280);
     QDesktopWidget *desktop = QApplication::desktop();
@@ -66,7 +73,99 @@ OsdWidget::~OsdWidget()
 
 void OsdWidget::showAll() 
 {
-    show();
+    unsigned int outputConnected = 0;
+    bool hasPrimary = false;
+
+    for (KScreen::OutputPtr &output : m_config->outputs()) {
+        if (output->isPrimary() || output->name().contains(lvdsPrefix))
+            hasPrimary = true;
+
+        if (output->isConnected())
+            outputConnected++;
+    }
+
+    if (hasPrimary && outputConnected == 2) {
+        show();
+    }
+
+    if (outputConnected > 2) {
+        KToolInvocation::kdeinitExec(QString("kcmshell5"),
+                                     QStringList() << QString("kcm_kscreen"));
+    }
+}
+
+void OsdWidget::m_pcScreenOnly() 
+{
+    SetConfigOpThread *thread = nullptr;
+    
+    for (KScreen::OutputPtr &output : m_config->outputs()) {
+        // if there is NO primary set, isPrimary is unreliable!
+        if (output->isPrimary() || output->name().contains(lvdsPrefix))
+            output->setEnabled(true);
+        else
+            output->setEnabled(false);
+    }
+
+    thread = new SetConfigOpThread(m_config);
+    thread->start();
+}
+
+void OsdWidget::m_mirror() 
+{
+    KScreen::OutputPtr primaryOutput;
+    QPoint primaryPos(0, 0);
+    SetConfigOpThread *thread = nullptr;
+
+    // TODO: it needs to find the same resoluation, if there is none?
+    for (KScreen::OutputPtr &output : m_config->outputs()) {
+        if (output->isPrimary() || output->name().contains(lvdsPrefix)) {
+            primaryOutput = output;
+            output->setPos(primaryPos);
+        } else {
+            output->setPos(primaryPos);
+        }
+    }
+
+    thread = new SetConfigOpThread(m_config);
+    thread->start();
+}
+
+void OsdWidget::m_extend() 
+{
+    QPoint secondPos(0, 0);
+    SetConfigOpThread *thread = nullptr;
+
+    for (KScreen::OutputPtr &output : m_config->outputs()) {
+        if (output->isPrimary() || output->name().contains(lvdsPrefix)) {
+            output->setPos(secondPos);
+            secondPos.setX(output->pos().x() + output->size().width());
+        } else {
+            output->setPos(secondPos);
+        }
+    }
+
+    if (secondPos.isNull())
+        return;
+
+    thread = new SetConfigOpThread(m_config);
+    thread->start();
+}
+
+void OsdWidget::m_secondScreenOnly() 
+{
+    SetConfigOpThread *thread = nullptr;
+    
+    for (KScreen::OutputPtr &output : m_config->outputs()) {
+        if (output->isPrimary() || output->name().contains(lvdsPrefix)) {
+            output->setPrimary(false);
+            output->setEnabled(false);
+        } else {
+            output->setEnabled(true);
+        }
+    }
+
+    thread = new SetConfigOpThread(m_config);
+    thread->start();
 }
 
 void OsdWidget::slotItemClicked(QListWidgetItem *item) 
@@ -74,12 +173,26 @@ void OsdWidget::slotItemClicked(QListWidgetItem *item)
     hide();
 
     if (item->text() == PC_SCREEN_ONLY_MODE) {
-        emit pcScreenOnly();
+        m_pcScreenOnly();
     } else if (item->text() == MIRROR_MODE) {
-        emit mirror();
+        m_mirror();
     } else if (item->text() == EXTEND_MODE) {
-        emit extend();
+        m_extend();
     } else if (item->text() == SECOND_SCREEN_ONLY_MODE) {
-        emit secondScreenOnly();
+        m_secondScreenOnly();
     }
 }
+
+SetConfigOpThread::SetConfigOpThread(KScreen::ConfigPtr config) 
+  : m_config(config) 
+{
+}
+
+void SetConfigOpThread::run() 
+{
+    //if (!KScreen::Config::canBeApplied(m_config))
+    //    return;
+
+    auto *op = new KScreen::SetConfigOperation(m_config);
+    op->exec();
+}
diff --git a/kded/osdwidget.h b/kded/osdwidget.h
index 8fab165..442f978 100644
--- a/kded/osdwidget.h
+++ b/kded/osdwidget.h
@@ -21,26 +21,48 @@
 
 #include <QWidget>
 #include <QListWidgetItem>
+#include <QThread>
+
+#include <kscreen/config.h>
+
+const QString lvdsPrefix = "LVDS";
 
 class OsdWidget : public QWidget 
 {
     Q_OBJECT
 
 public:
-    explicit OsdWidget(QWidget *parent = nullptr, 
+    explicit OsdWidget(KScreen::ConfigPtr config, 
+                       QWidget *parent = nullptr, 
                        Qt::WindowFlags f = Qt::ToolTip);
     ~OsdWidget();
 
     void showAll();
 
-Q_SIGNALS:
-    void pcScreenOnly();
-    void mirror();
-    void extend();
-    void secondScreenOnly();
-
 private slots:
     void slotItemClicked(QListWidgetItem*);
+
+private:
+    void m_pcScreenOnly();
+    void m_mirror();
+    void m_extend();
+    void m_secondScreenOnly();
+
+    KScreen::ConfigPtr m_config;
+};
+
+class SetConfigOpThread : public QThread
+{
+    Q_OBJECT
+
+public:
+    explicit SetConfigOpThread(KScreen::ConfigPtr);
+
+protected:
+    void run();
+
+private:
+    KScreen::ConfigPtr m_config;
 };
 
 #endif /* __OSD_WIDGET_H__ */
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic