Add BeRTOS header in sources files
[bertos.git] / wizard / BToolchainSearch.py
1 #!/usr/bin/env python
2 # encoding: utf-8
3 #
4 # This file is part of BeRTOS.
5 #
6 # Bertos 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 2 of the License, or
9 # (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 St, Fifth Floor, Boston, MA  02110-1301  USA
19 #
20 # As a special exception, you may use this file as part of a free software
21 # library without restriction.  Specifically, if other files instantiate
22 # templates or use macros or inline functions from this file, or you compile
23 # this file and link it with other files to produce an executable, this
24 # file does not by itself cause the resulting executable to be covered by
25 # the GNU General Public License.  This exception does not however
26 # invalidate any other reasons why the executable file might be covered by
27 # the GNU General Public License.
28 #
29 # Copyright 2008 Develer S.r.l. (http://www.develer.com/)
30 #
31 # $Id$
32 #
33 # Author: Lorenzo Berni <duplo@develer.com>
34 #
35
36 from PyQt4.QtCore import *
37 from PyQt4.QtGui import *
38 import PyQt4.uic as uic
39
40 import qvariant_converter
41
42 from const import *
43
44 class BToolchainSearch(QDialog):
45     """
46     Dialog that permits to choice the settings for the toolchain search procedure.
47     """
48     
49     def __init__(self):
50         QDialog.__init__(self)
51         self._setupUi()
52         self._connectSignals()
53         self.setWindowTitle(self.tr("Toolchain search page"))
54     
55     def _setupUi(self):
56         """
57         Sets up the user interface.
58         """
59         self.content = uic.loadUi(UI_LOCATION + "/toolchain_search.ui", None)
60         layout = QVBoxLayout()
61         layout.addWidget(self.content)
62         self.setLayout(layout)
63         self._populateDirList()
64         self._setPathSearch()
65         self._setSearchButton()
66     
67     def _connectSignals(self):
68         """
69         Connects the signals with the related slots.
70         """
71         self.connect(self.content.pathBox, SIGNAL("stateChanged(int)"), self._stateChanged)
72         self.connect(self.content.addButton, SIGNAL("clicked()"), self._addDir)
73         self.connect(self.content.removeButton, SIGNAL("clicked()"), self._removeDir)
74         self.connect(self.content.cancelButton, SIGNAL("clicked()"), self.reject)
75         self.connect(self.content.searchButton, SIGNAL("clicked()"), self.accept)
76         
77     def _setSearchButton(self):
78         """
79         Toggles the 'searchButton' evaluating the settings.
80         """
81         self.content.searchButton.setDefault(True)
82         self.content.searchButton.setEnabled(self.content.pathBox.isChecked() or self.content.customDirList.count() != 0)
83     
84     def _populateDirList(self):
85         """
86         Fills the dir list with the directories stored in the QSettings.
87         """
88         search_dir_list = qvariant_converter.getStringList(QApplication.instance().settings.value("search_dir_list"))
89         for element in search_dir_list:
90             item = QListWidgetItem(element)
91             self.content.customDirList.addItem(item)
92     
93     def _setPathSearch(self):
94         """
95         Sets the path search checkbox to the stored value.
96         """
97         pathSearch = qvariant_converter.getBool(QApplication.instance().settings.value(QString("path_search")))
98         self.content.pathBox.setChecked(pathSearch)
99     
100     def _stateChanged(self, state):
101         """
102         Slot called when the path search checkbox state changes. Stores the value in the QSettings.
103         """
104         QApplication.instance().settings.setValue(QString("path_search"), QVariant(state != 0))
105         self._setSearchButton()
106     
107     def _addDir(self):
108         """
109         Slot called when the user adds a dir.
110         """
111         directory = QFileDialog.getExistingDirectory(self, self.tr("Open Directory"), "", QFileDialog.ShowDirsOnly)
112         if not directory.isEmpty():
113             directory = unicode(directory)
114             item = QListWidgetItem(directory)
115             self.content.customDirList.addItem(item)
116             search_dir_list = qvariant_converter.getStringList(QApplication.instance().settings.value("search_dir_list"))
117             search_dir_list = set(search_dir_list + [directory])
118             QApplication.instance().settings.setValue(QString("search_dir_list"), qvariant_converter.convertStringList(list(search_dir_list)))
119             self._setSearchButton()
120     
121     def _removeDir(self):
122         """
123         Slot called when the user removes a dir.
124         """
125         if self.content.customDirList.currentRow() != -1:
126             item = self.content.customDirList.takeItem(self.content.customDirList.currentRow())
127             search_dir_list = qvariant_converter.getStringList(QApplication.instance().settings.value(QString("search_dir_list")))
128             search_dir_list = set(search_dir_list)
129             search_dir_list.remove(unicode(item.text()))
130             QApplication.instance().settings.setValue(QString("search_dir_list"), qvariant_converter.convertStringList(list(search_dir_list)))
131             self._setSearchButton()
132