From ee1f6b099a234d0119f66d227885c5e9d394c233 Mon Sep 17 00:00:00 2001
From: Andreas Traczyk <andreas.traczyk@savoirfairelinux.com>
Date: Thu, 14 Feb 2019 09:42:47 -0500
Subject: [PATCH] video models: add a way to set resolution and rate
 simultaneously

Change-Id: I5c6e4e4733a452ff770c1ac4dbe997cb568c2b41
---
 src/video/channel.cpp    | 20 +++++++++++++++++++-
 src/video/channel.h      |  3 +++
 src/video/resolution.cpp |  2 +-
 3 files changed, 23 insertions(+), 2 deletions(-)

diff --git a/src/video/channel.cpp b/src/video/channel.cpp
index 70e87ac4..2fe55ce7 100644
--- a/src/video/channel.cpp
+++ b/src/video/channel.cpp
@@ -19,6 +19,7 @@
 
 //Ring
 #include "resolution.h"
+#include "rate.h"
 #include "device.h"
 #include "../dbus/videomanager.h"
 #include "../private/videochannel_p.h"
@@ -81,7 +82,7 @@ bool Video::Channel::setActiveResolution(int idx)
 
 bool Video::Channel::setActiveResolution(Video::Resolution* res) {
    if ((!res) || d_ptr->m_lValidResolutions.indexOf(res) == -1 || res->name().isEmpty()) {
-      qWarning() << "Invalid active resolution" << (res?res->name():"NULL");
+      qWarning() << "Invalid active resolution: " << (res?res->name():"NULL");
       return false;
    }
 
@@ -93,6 +94,23 @@ bool Video::Channel::setActiveResolution(Video::Resolution* res) {
    return true;
 }
 
+bool Video::Channel::setActiveMode(int resIndex, int rateIndex) {
+    if (resIndex < 0 || resIndex >= d_ptr->m_lValidResolutions.size()) return false;
+    auto res = d_ptr->m_lValidResolutions[resIndex];
+    if (rateIndex < 0 || rateIndex >= res->validRates().size()) return false;
+    auto rate = res->validRates()[rateIndex];
+
+    if (d_ptr->m_pCurrentResolution == res &&
+        res->activeRate() == rate) {
+        qWarning() << "Mode already set: " << (res ? res->name() : "NULL") << (rate ? rate->name() : "NULL");
+        return false;
+    }
+
+    d_ptr->m_pCurrentResolution = res;
+    d_ptr->m_pCurrentResolution->setActiveRate(rate);
+    return true;
+}
+
 Video::Resolution* Video::Channel::activeResolution()
 {
    //If it is the current device, then there is "current" resolution
diff --git a/src/video/channel.h b/src/video/channel.h
index 257f805d..70d3b6a7 100644
--- a/src/video/channel.h
+++ b/src/video/channel.h
@@ -25,6 +25,7 @@
 //Ring
 namespace Video {
    class Resolution;
+   class Rate;
    class Device;
 }
 class VideoChannelPrivate;
@@ -46,6 +47,8 @@ public:
    bool setActiveResolution(Video::Resolution* res);
    bool setActiveResolution(int idx);
 
+   bool setActiveMode(int resIndex, int rateIndex);
+
    //Model
    virtual QVariant      data     ( const QModelIndex& index, int role = Qt::DisplayRole     ) const override;
    virtual int           rowCount ( const QModelIndex& parent = QModelIndex()                ) const override;
diff --git a/src/video/resolution.cpp b/src/video/resolution.cpp
index 255315e2..434fc6f7 100644
--- a/src/video/resolution.cpp
+++ b/src/video/resolution.cpp
@@ -89,7 +89,7 @@ const QList<Video::Rate*> Video::Resolution::validRates() const {
 
 bool Video::Resolution::setActiveRate(Video::Rate* rate) {
    if (!rate || (d_ptr->m_lValidRates.indexOf(rate) == -1)) {
-      qWarning() << "Trying to set an invalid rate" << rate;
+      qWarning() << "Trying to set an invalid rate: " << rate->name();
       return false;
    }
 
-- 
GitLab