LCOV - code coverage report
Current view: top level - src/tools/qt-gui/src - guisettings.cpp (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 0 131 0.0 %
Date: 2019-09-12 12:28:41 Functions: 0 14 0.0 %

          Line data    Source code
       1             : /**
       2             :  * @file
       3             :  *
       4             :  * @brief
       5             :  *
       6             :  * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
       7             :  */
       8             : 
       9             : #include "guisettings.hpp"
      10             : #include <QDebug>
      11             : #include <QPalette>
      12             : 
      13             : using namespace kdb;
      14             : 
      15             : namespace kdb
      16             : {
      17             : template <>
      18           0 : inline QColor Key::get () const
      19             : {
      20           0 :         if (getStringSize () < 1)
      21             :         {
      22           0 :                 throw KeyTypeConversion ();
      23             :         }
      24             : 
      25           0 :         std::string str = getString ();
      26           0 :         QColor c (str.c_str ());
      27           0 :         return c;
      28             : }
      29             : } // namespace kdb
      30             : 
      31           0 : void GUISettings::setDefaults ()
      32             : {
      33           0 :         QPalette palette;
      34           0 :         palette.setCurrentColorGroup (QPalette::Active);
      35             : 
      36           0 :         m_highlightColor = palette.highlight ().color ();
      37           0 :         m_frameColor = palette.dark ().color ();
      38           0 :         m_nodeWithKeyColor = palette.windowText ().color ();
      39           0 :         m_nodeWithoutKeyColor = palette.windowText ().color ();
      40             : 
      41           0 :         palette.setCurrentColorGroup (QPalette::Disabled);
      42             : 
      43           0 :         m_useSystemIconTheme = true;
      44           0 :         m_viewermode = false;
      45           0 : }
      46             : 
      47           0 : GUISettings::GUISettings (QObject * parentGUISettings)
      48             : : QObject (parentGUISettings), m_profile ("/current/"), m_base ("/sw/elektra/qtgui/#0/"), m_highlightColorString ("color/highlight"),
      49             :   m_frameColorString ("color/frame"), m_nodeWKeyColorString ("color/node/with"), m_nodeWOKeyColorString ("color/node/without"),
      50             :   m_useSystemIconThemeString ("icon/system"), m_legacyBase ("/sw/libelektra.org/qt-gui/#0/"),
      51             :   m_legacyHighlightColorString ("highlight_color"), m_legacyFrameColorString ("frame_color"),
      52             :   m_legacyNodeWKeyColorString ("node_with_key_color"), m_legacyNodeWOKeyColorString ("node_without_key_color"),
      53           0 :   m_viewermodeString ("mode/viewer")
      54             : {
      55             :         // initialize with hardcoded default colors
      56           0 :         setDefaults ();
      57             : 
      58             :         // check if stored colors exist, if so, load them
      59           0 :         getKDB ();
      60           0 : }
      61             : 
      62           0 : QColor GUISettings::highlightColor () const
      63             : {
      64           0 :         return m_highlightColor;
      65             : }
      66             : 
      67           0 : QColor GUISettings::frameColor () const
      68             : {
      69           0 :         return m_frameColor;
      70             : }
      71             : 
      72           0 : QColor GUISettings::nodeWithKeyColor () const
      73             : {
      74           0 :         return m_nodeWithKeyColor;
      75             : }
      76             : 
      77           0 : QColor GUISettings::nodeWithoutKeyColor () const
      78             : {
      79           0 :         return m_nodeWithoutKeyColor;
      80             : }
      81             : 
      82             : 
      83           0 : bool GUISettings::useSystemIconTheme () const
      84             : {
      85           0 :         return m_useSystemIconTheme;
      86             : }
      87             : 
      88           0 : bool GUISettings::viewermode () const
      89             : {
      90           0 :         return m_viewermode;
      91             : }
      92             : 
      93           0 : void GUISettings::setHighlightColor (const QColor & color)
      94             : {
      95           0 :         if (color != m_highlightColor)
      96             :         {
      97           0 :                 m_highlightColor = color;
      98           0 :                 appendColor (m_highlightColorString, color);
      99             : 
     100           0 :                 emit highlightColorChanged ();
     101             :         }
     102           0 : }
     103             : 
     104           0 : void GUISettings::setFrameColor (const QColor & color)
     105             : {
     106           0 :         if (color != m_frameColor)
     107             :         {
     108           0 :                 m_frameColor = color;
     109           0 :                 appendColor (m_frameColorString, color);
     110             : 
     111           0 :                 emit frameColorChanged ();
     112             :         }
     113           0 : }
     114             : 
     115           0 : void GUISettings::setNodeWithKeyColor (const QColor & color)
     116             : {
     117           0 :         if (color != m_nodeWithKeyColor)
     118             :         {
     119           0 :                 m_nodeWithKeyColor = color;
     120           0 :                 appendColor (m_nodeWKeyColorString, color);
     121             : 
     122           0 :                 emit nodeWithKeyColorChanged ();
     123             :         }
     124           0 : }
     125             : 
     126           0 : void GUISettings::setNodeWithoutKeyColor (const QColor & color)
     127             : {
     128           0 :         if (color != m_nodeWithoutKeyColor)
     129             :         {
     130           0 :                 m_nodeWithoutKeyColor = color;
     131           0 :                 appendColor (m_nodeWOKeyColorString, color);
     132             : 
     133           0 :                 emit nodeWithoutKeyColorChanged ();
     134             :         }
     135           0 : }
     136             : 
     137           0 : void GUISettings::useSystemIconTheme (const bool & use)
     138             : {
     139           0 :         if (use != m_useSystemIconTheme)
     140             :         {
     141           0 :                 m_useSystemIconTheme = use;
     142           0 :                 appendBool (m_useSystemIconThemeString, use);
     143           0 :                 emit useSystemIconThemeChanged ();
     144             :         }
     145           0 : }
     146             : 
     147           0 : void GUISettings::setViewermode (bool vmode)
     148             : {
     149           0 :         if (vmode != m_viewermode)
     150             :         {
     151           0 :                 m_viewermode = vmode;
     152           0 :                 appendBool (m_viewermodeString, vmode);
     153           0 :                 emit viewermodeChanged ();
     154             :         }
     155           0 : }
     156             : 
     157           0 : void GUISettings::appendColor (const std::string & keyName, const QColor & color)
     158             : {
     159           0 :         std::string name = "user" + m_base + m_profile + keyName;
     160           0 :         m_config.append (Key (name, KEY_VALUE, color.name ().toStdString ().c_str (), KEY_END));
     161           0 : }
     162             : 
     163           0 : void GUISettings::appendBool (const std::string & keyName, const bool value)
     164             : {
     165           0 :         std::string name = "user" + m_base + m_profile + keyName;
     166           0 :         Key key = m_config.lookup (name);
     167           0 :         if (key)
     168             :         {
     169           0 :                 key.set<bool> (value);
     170             :         }
     171             :         else
     172             :         {
     173           0 :                 Key k;
     174           0 :                 k.setName (name);
     175           0 :                 k.set<bool> (value);
     176           0 :                 m_config.append (k);
     177             :         }
     178           0 : }
     179             : 
     180           0 : void GUISettings::lookupColor (const std::string & keyName, QColor & toSet) const
     181             : {
     182           0 :         Key key = m_config.lookup (keyName);
     183             : 
     184           0 :         if (!key) return; // nothing to do
     185             : 
     186             :         try
     187             :         {
     188           0 :                 toSet = key.get<QColor> ();
     189             :         }
     190           0 :         catch (const KeyTypeConversion & ex)
     191             :         {
     192           0 :                 qDebug () << ex.what ();
     193             :         }
     194             : }
     195             : 
     196           0 : void GUISettings::lookupBool (const std::string & keyName, bool & value) const
     197             : {
     198           0 :         Key key = m_config.lookup (keyName);
     199             : 
     200           0 :         if (!key) return; // nothing to do
     201             : 
     202             :         try
     203             :         {
     204           0 :                 value = (key.get<bool> ());
     205             :         }
     206           0 :         catch (const KeyTypeConversion & ex)
     207             :         {
     208           0 :                 qDebug () << ex.what ();
     209             :         }
     210             : }
     211             : 
     212             : 
     213           0 : void GUISettings::setKDB ()
     214             : {
     215           0 :         KDB kdb;
     216           0 :         KeySet dummySet; // cannot set without get
     217             : 
     218             :         try
     219             :         {
     220           0 :                 kdb.get (dummySet, m_base);
     221             :         }
     222           0 :         catch (const KDBException & ex)
     223             :         {
     224           0 :                 qDebug () << tr ("Could not read from database, unable to retrieve settings. The system responds: %1").arg (ex.what ());
     225             :         }
     226             : 
     227             :         // won't set config without user prefix
     228             :         try
     229             :         {
     230           0 :                 kdb.set (m_config, "user" + m_base);
     231             :         }
     232           0 :         catch (const KDBException & ex)
     233             :         {
     234           0 :                 qDebug () << tr ("Could not write to database, unable to store settings. The system responds: %1").arg (ex.what ());
     235             :         }
     236           0 : }
     237             : 
     238           0 : void GUISettings::getKDB ()
     239             : {
     240           0 :         KDB kdb;
     241             : 
     242             :         // retrieve keys below base path
     243             :         try
     244             :         {
     245           0 :                 kdb.get (m_config, m_legacyBase);
     246           0 :                 kdb.get (m_config, m_base);
     247             :         }
     248           0 :         catch (const KDBException & ex)
     249             :         {
     250           0 :                 qDebug () << tr ("Could not read from database, unable to retrieve settings. The system responds: %1").arg (ex.what ());
     251             :         }
     252             : 
     253             :         // (1) first with legacy:
     254           0 :         lookupColor (m_legacyBase + m_legacyHighlightColorString, m_highlightColor);
     255           0 :         lookupColor (m_legacyBase + m_legacyFrameColorString, m_frameColor);
     256           0 :         lookupColor (m_legacyBase + m_legacyNodeWKeyColorString, m_nodeWithKeyColor);
     257           0 :         lookupColor (m_legacyBase + m_legacyNodeWOKeyColorString, m_nodeWithoutKeyColor);
     258             : 
     259           0 :         for (int i = 0; i < 2; ++i)
     260             :         {
     261           0 :                 std::string profile;
     262           0 :                 switch (i)
     263             :                 {
     264             :                 case 0:
     265             :                         profile = "%/";
     266             :                         break; // (2) then with fallback profile
     267             :                 case 1:
     268           0 :                         profile = m_profile;
     269             :                         break; // (3) and finally with current profile
     270             :                 }
     271           0 :                 lookupColor (m_base + profile + m_highlightColorString, m_highlightColor);
     272           0 :                 lookupColor (m_base + profile + m_frameColorString, m_frameColor);
     273           0 :                 lookupColor (m_base + profile + m_nodeWKeyColorString, m_nodeWithKeyColor);
     274           0 :                 lookupColor (m_base + profile + m_nodeWOKeyColorString, m_nodeWithoutKeyColor);
     275           0 :                 lookupBool (m_base + profile + m_useSystemIconThemeString, m_useSystemIconTheme);
     276           0 :                 lookupBool (m_base + profile + m_viewermodeString, m_viewermode);
     277             :         }
     278           0 : }
     279             : 
     280           0 : void GUISettings::reset ()
     281             : {
     282           0 :         setDefaults ();
     283           0 :         setKDB ();
     284           0 : }

Generated by: LCOV version 1.13