1 /**
2  * UGENE - Integrated Bioinformatics Tools.
3  * Copyright (C) 2008-2021 UniPro <ugene@unipro.ru>
4  * http://ugene.net
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program 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 this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19  * MA 02110-1301, USA.
20  */
21 
22 #include "ColorSchemaSettingsController.h"
23 
24 #include <QColor>
25 #include <QDir>
26 
27 #include <U2Algorithm/ColorSchemeUtils.h>
28 
29 #include <U2Core/AppContext.h>
30 #include <U2Core/AppSettings.h>
31 #include <U2Core/DNAAlphabet.h>
32 #include <U2Core/IOAdapter.h>
33 #include <U2Core/Log.h>
34 #include <U2Core/Settings.h>
35 #include <U2Core/UserApplicationsSettings.h>
36 
37 #include <U2Gui/HelpButton.h>
38 
39 #include "ColorSchemaDialogController.h"
40 
41 namespace U2 {
42 
setSchemaColors(const ColorSchemeData & customSchema)43 static void setSchemaColors(const ColorSchemeData &customSchema) {
44     QString dirPath = ColorSchemeUtils::getColorsDir();
45 
46     QDir dir(dirPath);
47     if (!dir.exists()) {
48         dir.mkpath(".");
49     }
50 
51     IOAdapterFactory *factory = AppContext::getIOAdapterRegistry()->getIOAdapterFactoryById(BaseIOAdapters::LOCAL_FILE);
52 
53     QScopedPointer<IOAdapter> io(factory->createIOAdapter());
54 
55     const QMap<char, QColor> &alphColors = customSchema.alpColors;
56     bool defaultType = customSchema.defaultAlpType;
57 
58     QString keyword(customSchema.type == DNAAlphabet_AMINO ? ColorSchemeUtils::COLOR_SCHEME_AMINO_KEYWORD : (defaultType ? ColorSchemeUtils::COLOR_SCHEME_NUCL_DEFAULT_KEYWORD : ColorSchemeUtils::COLOR_SCHEME_NUCL_EXTENDED_KEYWORD));
59 
60     QString url = dir.filePath(customSchema.name + ColorSchemeUtils::COLOR_SCHEME_NAME_FILTERS);
61     bool ok = io->open(url, IOAdapterMode_Write);
62     if (!ok) {
63         uiLog.error(ColorSchemaSettingsPageWidget::tr("Failed to save schema file: '%1'").arg(url));
64         return;
65     }
66 
67     // write header
68     QByteArray header;
69     header.append(QString("%1\n").arg(keyword));
70     io->writeBlock(header);
71     // write body
72     QMapIterator<char, QColor> it(alphColors);
73     while (it.hasNext()) {
74         it.next();
75         QByteArray line;
76         line.append(QString("%1=%2\n").arg(it.key()).arg(it.value().name()));
77         io->writeBlock(line);
78     }
79 }
80 
81 const QString ColorSchemaSettingsPageController::helpPageId = QString("65929359");
82 
ColorSchemaSettingsPageController(MsaColorSchemeRegistry * mcsr,QObject * p)83 ColorSchemaSettingsPageController::ColorSchemaSettingsPageController(MsaColorSchemeRegistry *mcsr, QObject *p)
84     : AppSettingsGUIPageController(tr("Alignment Color Scheme"), ColorSchemaSettingsPageId, p) {
85     connect(this, SIGNAL(si_customSettingsChanged()), mcsr, SLOT(sl_onCustomSettingsChanged()));
86 }
87 
getSavedState()88 AppSettingsGUIPageState *ColorSchemaSettingsPageController::getSavedState() {
89     ColorSchemaSettingsPageState *state = new ColorSchemaSettingsPageState();
90     state->colorsDir = ColorSchemeUtils::getColorsDir();
91     state->customSchemas = ColorSchemeUtils::getSchemas();
92 
93     return state;
94 }
95 
saveState(AppSettingsGUIPageState * s)96 void ColorSchemaSettingsPageController::saveState(AppSettingsGUIPageState *s) {
97     ColorSchemaSettingsPageState *state = qobject_cast<ColorSchemaSettingsPageState *>(s);
98 
99     ColorSchemeUtils::setColorsDir(state->colorsDir);
100     QDir dir(ColorSchemeUtils::getColorsDir());
101     foreach (const ColorSchemeData &schema, state->removedCustomSchemas) {
102         dir.remove(schema.name + ColorSchemeUtils::COLOR_SCHEME_NAME_FILTERS);
103     }
104     foreach (const ColorSchemeData &schema, state->customSchemas) {
105         setSchemaColors(schema);
106     }
107     emit si_customSettingsChanged();
108 }
109 
createWidget(AppSettingsGUIPageState * state)110 AppSettingsGUIPageWidget *ColorSchemaSettingsPageController::createWidget(AppSettingsGUIPageState *state) {
111     ColorSchemaSettingsPageWidget *r = new ColorSchemaSettingsPageWidget(this);
112     r->setState(state);
113     return r;
114 }
115 
116 }  // namespace U2
117