xref: /glogg/src/configuration.cpp (revision 80bca0a387968c28827e5f6a97058c7bbfcfbd38)
1 /*
2  * Copyright (C) 2009, 2010, 2013, 2015 Nicolas Bonnefon and other contributors
3  *
4  * This file is part of glogg.
5  *
6  * glogg is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * glogg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with glogg.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include <QFontInfo>
21 
22 #include "log.h"
23 
24 #include "configuration.h"
25 
26 Configuration::Configuration()
27 {
28     // Should have some sensible default values.
29     mainFont_ = QFont("mono", 10);
30     mainFont_.setStyleHint( QFont::Courier, QFont::PreferOutline );
31 
32     mainRegexpType_               = ExtendedRegexp;
33     quickfindRegexpType_          = FixedString;
34     quickfindIncremental_         = true;
35 
36 #ifdef GLOGG_SUPPORTS_POLLING
37     pollingEnabled_               = true;
38 #else
39     pollingEnabled_               = false;
40 #endif
41     pollIntervalMs_               = 2000;
42 
43     overviewVisible_              = true;
44     lineNumbersVisibleInMain_     = false;
45     lineNumbersVisibleInFiltered_ = true;
46 
47     QFontInfo fi(mainFont_);
48     LOG(logDEBUG) << "Default font is " << fi.family().toStdString();
49 
50     searchAutoRefresh_ = false;
51     searchIgnoreCase_  = false;
52 }
53 
54 // Accessor functions
55 QFont Configuration::mainFont() const
56 {
57     return mainFont_;
58 }
59 
60 void Configuration::setMainFont( QFont newFont )
61 {
62     LOG(logDEBUG) << "Configuration::setMainFont";
63 
64     mainFont_ = newFont;
65 }
66 
67 void Configuration::retrieveFromStorage( QSettings& settings )
68 {
69     LOG(logDEBUG) << "Configuration::retrieveFromStorage";
70 
71     // Fonts
72     QString family = settings.value( "mainFont.family" ).toString();
73     int size = settings.value( "mainFont.size" ).toInt();
74 
75     // If no config read, keep the default
76     if ( !family.isNull() )
77         mainFont_ = QFont( family, size );
78 
79     // Regexp types
80     mainRegexpType_ = static_cast<SearchRegexpType>(
81             settings.value( "regexpType.main", mainRegexpType_ ).toInt() );
82     quickfindRegexpType_ = static_cast<SearchRegexpType>(
83             settings.value( "regexpType.quickfind", quickfindRegexpType_ ).toInt() );
84     if ( settings.contains( "quickfind.incremental" ) )
85         quickfindIncremental_ = settings.value( "quickfind.incremental" ).toBool();
86 
87     // "Advanced" settings
88     if ( settings.contains( "polling.enabled" ) )
89         pollingEnabled_ = settings.value( "polling.enabled" ).toBool();
90     if ( settings.contains( "polling.intervalMs" ) )
91         pollIntervalMs_ = settings.value( "polling.intervalMs" ).toInt();
92 
93     // View settings
94     if ( settings.contains( "view.overviewVisible" ) )
95         overviewVisible_ = settings.value( "view.overviewVisible" ).toBool();
96     if ( settings.contains( "view.lineNumbersVisibleInMain" ) )
97         lineNumbersVisibleInMain_ =
98             settings.value( "view.lineNumbersVisibleInMain" ).toBool();
99     if ( settings.contains( "view.lineNumbersVisibleInFiltered" ) )
100         lineNumbersVisibleInFiltered_ =
101             settings.value( "view.lineNumbersVisibleInFiltered" ).toBool();
102 
103     // Some sanity check (mainly for people upgrading)
104     if ( quickfindIncremental_ )
105         quickfindRegexpType_ = FixedString;
106 
107     // Default crawler settings
108     if ( settings.contains( "defaultView.searchAutoRefresh" ) )
109         searchAutoRefresh_ = settings.value( "defaultView.searchAutoRefresh" ).toBool();
110     if ( settings.contains( "defaultView.searchIgnoreCase" ) )
111         searchIgnoreCase_ = settings.value( "defaultView.searchIgnoreCase" ).toBool();
112 }
113 
114 void Configuration::saveToStorage( QSettings& settings ) const
115 {
116     LOG(logDEBUG) << "Configuration::saveToStorage";
117 
118     QFontInfo fi(mainFont_);
119 
120     settings.setValue( "mainFont.family", fi.family() );
121     settings.setValue( "mainFont.size", fi.pointSize() );
122     settings.setValue( "regexpType.main", static_cast<int>( mainRegexpType_ ) );
123     settings.setValue( "regexpType.quickfind", static_cast<int>( quickfindRegexpType_ ) );
124     settings.setValue( "quickfind.incremental", quickfindIncremental_ );
125     settings.setValue( "polling.enabled", pollingEnabled_ );
126     settings.setValue( "polling.intervalMs", pollIntervalMs_ );
127 
128     settings.setValue( "view.overviewVisible", overviewVisible_ );
129     settings.setValue( "view.lineNumbersVisibleInMain", lineNumbersVisibleInMain_ );
130     settings.setValue( "view.lineNumbersVisibleInFiltered", lineNumbersVisibleInFiltered_ );
131     settings.setValue( "defaultView.searchAutoRefresh", searchAutoRefresh_ );
132     settings.setValue( "defaultView.searchIgnoreCase", searchIgnoreCase_ );
133 }
134