xref: /glogg/src/configuration.cpp (revision f688be2ee6ce5760483290349a6e6227ac185220)
1 /*
2  * Copyright (C) 2009, 2010, 2013 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     overviewVisible_              = true;
37     lineNumbersVisibleInMain_     = false;
38     lineNumbersVisibleInFiltered_ = true;
39 
40     QFontInfo fi(mainFont_);
41     LOG(logDEBUG) << "Default font is " << fi.family().toStdString();
42 
43     searchAutoRefresh_ = false;
44     searchIgnoreCase_  = false;
45 }
46 
47 // Accessor functions
48 QFont Configuration::mainFont() const
49 {
50     return mainFont_;
51 }
52 
53 void Configuration::setMainFont( QFont newFont )
54 {
55     LOG(logDEBUG) << "Configuration::setMainFont";
56 
57     mainFont_ = newFont;
58 }
59 
60 void Configuration::retrieveFromStorage( QSettings& settings )
61 {
62     LOG(logDEBUG) << "Configuration::retrieveFromStorage";
63 
64     // Fonts
65     QString family = settings.value( "mainFont.family" ).toString();
66     int size = settings.value( "mainFont.size" ).toInt();
67 
68     // If no config read, keep the default
69     if ( !family.isNull() )
70         mainFont_ = QFont( family, size );
71 
72     // Regexp types
73     mainRegexpType_ = static_cast<SearchRegexpType>(
74             settings.value( "regexpType.main", mainRegexpType_ ).toInt() );
75     quickfindRegexpType_ = static_cast<SearchRegexpType>(
76             settings.value( "regexpType.quickfind", quickfindRegexpType_ ).toInt() );
77     if ( settings.contains( "quickfind.incremental" ) )
78         quickfindIncremental_ = settings.value( "quickfind.incremental" ).toBool();
79 
80     // View settings
81     if ( settings.contains( "view.overviewVisible" ) )
82         overviewVisible_ = settings.value( "view.overviewVisible" ).toBool();
83     if ( settings.contains( "view.lineNumbersVisibleInMain" ) )
84         lineNumbersVisibleInMain_ =
85             settings.value( "view.lineNumbersVisibleInMain" ).toBool();
86     if ( settings.contains( "view.lineNumbersVisibleInFiltered" ) )
87         lineNumbersVisibleInFiltered_ =
88             settings.value( "view.lineNumbersVisibleInFiltered" ).toBool();
89 
90     // Some sanity check (mainly for people upgrading)
91     if ( quickfindIncremental_ )
92         quickfindRegexpType_ = FixedString;
93 
94     // Default crawler settings
95     if ( settings.contains( "defaultView.searchAutoRefresh" ) )
96         searchAutoRefresh_ = settings.value( "defaultView.searchAutoRefresh" ).toBool();
97     if ( settings.contains( "defaultView.searchIgnoreCase" ) )
98         searchIgnoreCase_ = settings.value( "defaultView.searchIgnoreCase" ).toBool();
99 }
100 
101 void Configuration::saveToStorage( QSettings& settings ) const
102 {
103     LOG(logDEBUG) << "Configuration::saveToStorage";
104 
105     QFontInfo fi(mainFont_);
106 
107     settings.setValue( "mainFont.family", fi.family() );
108     settings.setValue( "mainFont.size", fi.pointSize() );
109     settings.setValue( "regexpType.main", static_cast<int>( mainRegexpType_ ) );
110     settings.setValue( "regexpType.quickfind", static_cast<int>( quickfindRegexpType_ ) );
111     settings.setValue( "quickfind.incremental", quickfindIncremental_ );
112     settings.setValue( "view.overviewVisible", overviewVisible_ );
113     settings.setValue( "view.lineNumbersVisibleInMain", lineNumbersVisibleInMain_ );
114     settings.setValue( "view.lineNumbersVisibleInFiltered", lineNumbersVisibleInFiltered_ );
115     settings.setValue( "defaultView.searchAutoRefresh", searchAutoRefresh_ );
116     settings.setValue( "defaultView.searchIgnoreCase", searchIgnoreCase_ );
117 }
118