def setUp(self):
     self.cem = ColorEntryManager()
     self.test_dict = {
         'RGBa(0, 0, 255, 255)': ('Fieldname1', 70, 20),
         'RGBa(0, 0, 220, 255)': ('Fieldname1', 70, 20)
     }
     self.test_layer = 'my_test_layer'
    def test_json_file_with_abbreviation_for_correctness(self):
        path = sys.path[0]
        cem = ColorEntryManager()
        layer_name = 'my_test_layer'
        abbreviation = '01TestLy'
        out_path = os.path.join(path, layer_name + '.txt')
        cem.add_layer(layer_name)

        dict = {'RGBa(0, 0, 255, 255)': ('name0', 120, 11)}
        dict['RGBa(0, 0, 255, 255)'] = ('name1', 131, 28)
        dict['RGBa(123, 21, 255, 255)'] = ('name2', 2, 8)
        dict['RGBa(2, 33, 25, 2)'] = ('name3', 1, 4)
        dict['RGBa(170, 12, 17, 36)'] = ('name4', 12, 238)

        cem.set_color_map_of_layer(dict, layer_name)
        cem.set_layer_abbreviation(layer_name, abbreviation)
        cem.write_map_to_disk(layer_name, out_path)
        self.assertTrue(os.path.exists(out_path))

        try:
            json_data = io.open(out_path)
            data = json.load(json_data)
            result_dict = {}
            for color, value_list in data['Legend'].iteritems():
                result_dict[color] = (value_list[0], value_list[1],
                                      value_list[2])

            self.assertEqual(data['Abbreviation'], abbreviation)
            self.assertDictContainsSubset(dict, result_dict)
            json_data.close()

        except IOError, Error:
            self.fail(Error)
Exemplo n.º 3
0
    def __init__(self):
        QtGui.QDialog.__init__(self, None, QtCore.Qt.WindowStaysOnTopHint)
        self.setupUi(self)
        self.color_entry_manager = ColorEntryManager()
        self.recent_layer = ''
        self.layers_dropdown.currentIndexChanged.connect(
            self.update_color_values)
        self.destroyed.connect(legend.nodeExitSolo)
        # set the table model
        self.header = ['Color value', 'Parameter Name', 'From', 'To', '']
        self.color_entry_manager.add_layer('dummy')
        self.color_entry_manager.add_color_value_quadruple_to_layer(
            ('RGBa(0, 0, 0, 0)', ' ', 0, 0), 'dummy')
        self.dummy_model = QColorTableModel(
            self.color_entry_manager.layer_values_map['dummy'], self.header,
            self)
        self.color_table_view.setModel(self.dummy_model)
        self.setup_table()

        # connect the signals
        self.color_table_view.clicked.connect(self.remove_entry)
        self.refresh_layers_dropdown.clicked.connect(self.refresh_layer_list)
        self.layers_dropdown.currentIndexChanged.connect(self.setup_legend)

        role = QtGui.QDialogButtonBox.ActionRole
        load_button = self.buttonBox.addButton('Load legend...', role)
        load_button.clicked.connect(self.load_color_map)
        save_button = self.buttonBox.addButton('Save legend', role)
        save_button.clicked.connect(self.save_color_map)
    def test_json_file_with_abbreviation_for_correctness(self):
        path = sys.path[0]
        cem = ColorEntryManager()
        layer_name = 'my_test_layer'
        abbreviation = '01TestLy'
        out_path = os.path.join(path, layer_name + '.txt')
        cem.add_layer(layer_name)

        dict = {'RGBa(0, 0, 255, 255)': ('name0', 120, 11)}
        dict['RGBa(0, 0, 255, 255)'] = ('name1', 131,28)
        dict['RGBa(123, 21, 255, 255)'] = ('name2', 2,8)
        dict['RGBa(2, 33, 25, 2)'] = ('name3', 1,4)
        dict['RGBa(170, 12, 17, 36)'] = ('name4', 12,238)

        cem.set_color_map_of_layer(dict, layer_name)
        cem.set_layer_abbreviation(layer_name, abbreviation)
        cem.write_map_to_disk(layer_name, out_path)
        self.assertTrue(os.path.exists(out_path))

        try:
            json_data = io.open(out_path)
            data = json.load(json_data)
            result_dict = {}
            for color, value_list in data['Legend'].iteritems():
                result_dict[color] = (value_list[0], value_list[1], value_list[2])

            self.assertEqual(data['Abbreviation'], abbreviation)
            self.assertDictContainsSubset(dict, result_dict)
            json_data.close()

        except IOError, Error:
            self.fail(Error)
Exemplo n.º 5
0
    def __init__(self):
        QtGui.QDialog.__init__(self, None, QtCore.Qt.WindowStaysOnTopHint)
        self.setupUi(self)
        self.color_entry_manager = ColorEntryManager()
        self.recent_layer = ''
        self.layers_dropdown.currentIndexChanged.connect(self.update_color_values)
        self.destroyed.connect(legend.nodeExitSolo)
        # set the table model
        self.header = ['Color value', 'Parameter Name', 'From', 'To', '']
        self.color_entry_manager.add_layer('dummy')
        self.color_entry_manager.add_color_value_quadruple_to_layer(('RGBa(0, 0, 0, 0)', ' ', 0, 0), 'dummy')
        self.dummy_model = QColorTableModel(self.color_entry_manager.layer_values_map['dummy'], self.header, self)
        self.color_table_view.setModel(self.dummy_model)
        self.setup_table()

        # connect the signals
        self.color_table_view.clicked.connect(self.remove_entry)
        self.refresh_layers_dropdown.clicked.connect(self.refresh_layer_list)
        self.layers_dropdown.currentIndexChanged.connect(self.setup_legend)

        role = QtGui.QDialogButtonBox.ActionRole
        load_button = self.buttonBox.addButton('Load legend...', role)
        load_button.clicked.connect(self.load_color_map)
        save_button = self.buttonBox.addButton('Save legend', role)
        save_button.clicked.connect(self.save_color_map)
    def test_cem_writes_map_only_if_layer_and_map_exist(self):
        path = sys.path[0]
        cem = ColorEntryManager()
        layer_name = 'my_test_layer'
        out_path = os.path.join(path, layer_name + '.txt')
        cem.write_map_to_disk(layer_name, out_path)
        self.assertFalse(os.path.exists(out_path))

        cem.add_layer(layer_name)
        cem.write_map_to_disk(layer_name, out_path)
        self.assertFalse(os.path.exists(out_path))
    def test_cem_writes_map_only_if_layer_and_map_exist(self):
        path = sys.path[0]
        cem = ColorEntryManager()
        layer_name = 'my_test_layer'
        out_path = os.path.join(path, layer_name + '.txt')
        cem.write_map_to_disk(layer_name, out_path)
        self.assertFalse(os.path.exists(out_path))

        cem.add_layer(layer_name)
        cem.write_map_to_disk(layer_name, out_path)
        self.assertFalse(os.path.exists(out_path))
    def test_cem_writes_map_to_correct_path(self):
        path = sys.path[0]
        cem = ColorEntryManager()
        layer_name = 'my_test_layer'
        cem.add_layer(layer_name)
        cem.add_color_value_quadruple_to_layer(('Color', 'name', 0, 1),
                                               layer_name)
        out_path = os.path.join(path, layer_name + '.txt')
        cem.write_map_to_disk(layer_name, out_path)

        self.assertTrue(os.path.exists(out_path))
        try:
            os.remove(out_path)
        except IOError, Error:
            print(Error)
    def test_cem_writes_map_to_correct_path(self):
        path = sys.path[0]
        cem = ColorEntryManager()
        layer_name = 'my_test_layer'
        cem.add_layer(layer_name)
        cem.add_color_value_quadruple_to_layer(('Color', 'name', 0, 1), layer_name)
        out_path = os.path.join(path, layer_name + '.txt')
        cem.write_map_to_disk(layer_name, out_path)

        self.assertTrue(os.path.exists(out_path))
        try:
            os.remove(out_path)
        except IOError, Error:
            print(Error)
 def setUp(self):
     self.cem = ColorEntryManager()
     self.test_dict = { 'RGBa(0, 0, 255, 255)' : ('Fieldname1', 70, 20), 'RGBa(0, 0, 220, 255)' : ('Fieldname1', 70, 20) }
     self.test_layer = 'my_test_layer'
class ColorEntryManagerTestCase(unittest.TestCase):

    def setUp(self):
        self.cem = ColorEntryManager()
        self.test_dict = { 'RGBa(0, 0, 255, 255)' : ('Fieldname1', 70, 20), 'RGBa(0, 0, 220, 255)' : ('Fieldname1', 70, 20) }
        self.test_layer = 'my_test_layer'

    def tearDown(self):
        path = sys.path[0]
        out_path = os.path.join(path, self.test_layer + '.txt')
        try:
            os.remove(out_path)
        except OSError:
            pass

    def test_dict_can_be_added_to_layer_in_color_entry_manager(self):
        self.cem.set_color_map_of_layer(self.test_dict, self.test_layer)
        self.assertDictContainsSubset({self.test_layer: self.test_dict}, self.cem.layer_values_map)

    def test_layer_can_be_passed_to_color_entry_manager(self):
        test_layer = 'my_test_layer.tif'
        self.cem.add_layer(test_layer)

        self.assertDictContainsSubset({test_layer: {}}, self.cem.layer_values_map)

    def test_layer_map_does_not_get_overwritten_in_cem(self):
        self.cem.set_color_map_of_layer(self.test_dict, self.test_layer)
        self.cem.add_layer(self.test_layer)
        self.assertDictContainsSubset({self.test_layer: self.test_dict}, self.cem.layer_values_map)

        self.test_dict['RGBa(0, 20, 255, 255)'] = ('Invalid', 47,11)
        self.assertNotEqual(self.cem.layer_values_map[self.test_layer], self.test_dict)

    def test_color_value_triple_can_be_added_to_layer_in_cem(self):
        self.cem.set_color_map_of_layer(self.test_dict, self.test_layer)
        color_value_quadruple = ('RGBa(0, 20, 255, 255)', 'name', 47, 11)

        self.cem.add_color_value_quadruple_to_layer(color_value_quadruple, self.test_layer)
        self.test_dict[color_value_quadruple[0]] = color_value_quadruple[1:]
        self.assertDictContainsSubset({self.test_layer: self.test_dict}, self.cem.layer_values_map)

    def test_cem_writes_map_to_correct_path(self):
        path = sys.path[0]
        cem = ColorEntryManager()
        layer_name = 'my_test_layer'
        cem.add_layer(layer_name)
        cem.add_color_value_quadruple_to_layer(('Color', 'name', 0, 1), layer_name)
        out_path = os.path.join(path, layer_name + '.txt')
        cem.write_map_to_disk(layer_name, out_path)

        self.assertTrue(os.path.exists(out_path))
        try:
            os.remove(out_path)
        except IOError, Error:
            print(Error)
class ColorEntryManagerTestCase(unittest.TestCase):
    def setUp(self):
        self.cem = ColorEntryManager()
        self.test_dict = {
            'RGBa(0, 0, 255, 255)': ('Fieldname1', 70, 20),
            'RGBa(0, 0, 220, 255)': ('Fieldname1', 70, 20)
        }
        self.test_layer = 'my_test_layer'

    def tearDown(self):
        path = sys.path[0]
        out_path = os.path.join(path, self.test_layer + '.txt')
        try:
            os.remove(out_path)
        except OSError:
            pass

    def test_dict_can_be_added_to_layer_in_color_entry_manager(self):
        self.cem.set_color_map_of_layer(self.test_dict, self.test_layer)
        self.assertDictContainsSubset({self.test_layer: self.test_dict},
                                      self.cem.layer_values_map)

    def test_layer_can_be_passed_to_color_entry_manager(self):
        test_layer = 'my_test_layer.tif'
        self.cem.add_layer(test_layer)

        self.assertDictContainsSubset({test_layer: {}},
                                      self.cem.layer_values_map)

    def test_layer_map_does_not_get_overwritten_in_cem(self):
        self.cem.set_color_map_of_layer(self.test_dict, self.test_layer)
        self.cem.add_layer(self.test_layer)
        self.assertDictContainsSubset({self.test_layer: self.test_dict},
                                      self.cem.layer_values_map)

        self.test_dict['RGBa(0, 20, 255, 255)'] = ('Invalid', 47, 11)
        self.assertNotEqual(self.cem.layer_values_map[self.test_layer],
                            self.test_dict)

    def test_color_value_triple_can_be_added_to_layer_in_cem(self):
        self.cem.set_color_map_of_layer(self.test_dict, self.test_layer)
        color_value_quadruple = ('RGBa(0, 20, 255, 255)', 'name', 47, 11)

        self.cem.add_color_value_quadruple_to_layer(color_value_quadruple,
                                                    self.test_layer)
        self.test_dict[color_value_quadruple[0]] = color_value_quadruple[1:]
        self.assertDictContainsSubset({self.test_layer: self.test_dict},
                                      self.cem.layer_values_map)

    def test_cem_writes_map_to_correct_path(self):
        path = sys.path[0]
        cem = ColorEntryManager()
        layer_name = 'my_test_layer'
        cem.add_layer(layer_name)
        cem.add_color_value_quadruple_to_layer(('Color', 'name', 0, 1),
                                               layer_name)
        out_path = os.path.join(path, layer_name + '.txt')
        cem.write_map_to_disk(layer_name, out_path)

        self.assertTrue(os.path.exists(out_path))
        try:
            os.remove(out_path)
        except IOError, Error:
            print(Error)
Exemplo n.º 13
0
class ColorPicker_dialog(QtGui.QDialog, Ui_color_picker_dialog):
    def __init__(self):
        QtGui.QDialog.__init__(self, None, QtCore.Qt.WindowStaysOnTopHint)
        self.setupUi(self)
        self.color_entry_manager = ColorEntryManager()
        self.recent_layer = ''
        self.layers_dropdown.currentIndexChanged.connect(
            self.update_color_values)
        self.destroyed.connect(legend.nodeExitSolo)
        # set the table model
        self.header = ['Color value', 'Parameter Name', 'From', 'To', '']
        self.color_entry_manager.add_layer('dummy')
        self.color_entry_manager.add_color_value_quadruple_to_layer(
            ('RGBa(0, 0, 0, 0)', ' ', 0, 0), 'dummy')
        self.dummy_model = QColorTableModel(
            self.color_entry_manager.layer_values_map['dummy'], self.header,
            self)
        self.color_table_view.setModel(self.dummy_model)
        self.setup_table()

        # connect the signals
        self.color_table_view.clicked.connect(self.remove_entry)
        self.refresh_layers_dropdown.clicked.connect(self.refresh_layer_list)
        self.layers_dropdown.currentIndexChanged.connect(self.setup_legend)

        role = QtGui.QDialogButtonBox.ActionRole
        load_button = self.buttonBox.addButton('Load legend...', role)
        load_button.clicked.connect(self.load_color_map)
        save_button = self.buttonBox.addButton('Save legend', role)
        save_button.clicked.connect(self.save_color_map)

    def setup_table(self):
        """
        Set the basic attributes for the color-table view
        :return:
        :rtype:
        """
        for i in range(len(self.header)):
            self.color_table_view.setItemDelegateForColumn(
                i, QColorTableDelegate(self))

        self.color_table_view.setColumnWidth(0, 205)
        self.color_table_view.setColumnWidth(1, 205)
        self.color_table_view.setColumnWidth(2, 75)
        self.color_table_view.setColumnWidth(3, 75)
        self.color_table_view.setColumnWidth(4, 35)
        self.color_table_view.setShowGrid(False)
        self.color_table_view.setSortingEnabled(True)
        self.color_table_view.sortByColumn(2)

        # set horizontal header properties
        hor_header = self.color_table_view.horizontalHeader()
        hor_header.setStretchLastSection(True)
        hor_header.setStyleSheet('QHeaderView::section { '
                                 ' border: None;'
                                 ' padding: 3px;'
                                 ' font-size: 13px;'
                                 ' font-weight: bold;'
                                 ' margin: 5px;'
                                 ' background-color: rgb(237, 237, 237);'
                                 '}')
        self.color_table_view.setHorizontalHeader(hor_header)

        # set row height
        ver_header = self.color_table_view.verticalHeader()
        ver_header.setDefaultSectionSize(34)
        ver_header.setStyleSheet('QHeaderView::section { '
                                 ' border: None;'
                                 ' padding: 3px;'
                                 ' font-size: 13px;'
                                 ' margin: 5px;'
                                 ' margin-right: 12px;'
                                 ' background-color: rgb(237, 237, 237);'
                                 '}')
        self.color_table_view.setVerticalHeader(ver_header)

    def remove_entry(self, model_index):
        """
        Remove the entry from the color-entry-manager
        :param model_index: The QModelIndex of the clicked element
        :type model_index: QModelIndex
        :return:
        :rtype:
        """
        column = model_index.column()
        if column == 4:
            row = model_index.row()
            model = model_index.model()
            color_key = model.in_data.keys()[row]
            layer = self.layers_dropdown.currentText()
            self.color_entry_manager.remove_color_entry_from_layer(
                color_key, layer)
            color_map = self.color_entry_manager.layer_values_map[layer]
            if not color_map:
                color_map = self.color_entry_manager.layer_values_map['dummy']
            tm = QColorTableModel(color_map, self.header, self)
            self.color_table_view.setModel(tm)

    def add_color(self, color):
        """
        Insert a new color (and the values associated to it, if any), into the color-table.
        :param color: The color in RGBa
        :type color: QColor
        :return:
        :rtype:
        """
        from mole import extensions

        layer = self.layers_dropdown.currentText()
        self.recent_layer = layer
        color_map = self.color_entry_manager.layer_values_map[layer]
        model = QColorTableModel(color_map, self.header, self)
        self.color_table_view.setModel(model)

        color_key = 'RGBa({}, {}, {}, {})'.format(color.red(), color.green(),
                                                  color.blue(), color.alpha())
        if color_map.has_key(color_key):
            self.message_label.setStyleSheet(_fromUtf8("color: red;"))
            self.message_label.setText(
                'Attention: Color {} is defined already.'.format(color_key))

        else:
            self.message_label.clear()
            #ToDo change to use the correct para-name instead
            #parameter_name = str(self.layers_dropdown.currentText())[:10]
            parameter_name = extensions.by_layername(layer)
            if not parameter_name: return None
            parameter_name = parameter_name[0].field_id
            self.color_entry_manager.add_color_value_quadruple_to_layer(
                [color_key, parameter_name, 0, 0], layer)
            color_map = self.color_entry_manager.layer_values_map[layer]
            model = QColorTableModel(color_map, self.header, self)
            self.color_table_view.setModel(model)
        self.raise_()

    def update_color_values(self):
        """
        Change the color-table view to display the color-map of the currently selected layer.
        :return:
        :rtype:
        """
        layer = self.layers_dropdown.currentText()
        color_map = {}
        if layer in self.color_entry_manager.layer_values_map:
            color_map = self.color_entry_manager.layer_values_map[layer]
            table_model = QColorTableModel(color_map, self.header, self)
        if not color_map:
            table_model = self.dummy_model
        self.color_table_view.setModel(table_model)
        self.recent_layer = layer

        try:
            wms_layer = legend.nodeByName(layer)[0].layer()
            url = wms_layer.legendUrl()
            self.legend_view.load(QtCore.QUrl(url))
        except (TypeError, AttributeError) as NoneTypeError:
            pass

    def check_character_constraint(self, parameter_name):
        """
        Check if the parameter-name has a length of ten characters at most,
        since layer-attributes are limited to 10 characters.
        :param parameter_name:
        :type parameter_name:
        :return:
        :rtype:
        """
        if len(parameter_name) == 10 and parameter_name != 'Parameter ':
            self.message_label.setStyleSheet(_fromUtf8("color: red;"))
            self.message_label.setText(
                'Warning: A maximum of 10 characters is allowed as a parameter name!'
            )
        else:
            self.message_label.clear()

    def load_color_map(self):
        """
        Load a color-map for the currently opened layer.
        :return:
        :rtype:
        """
        from mole import extensions

        layer = self.layers_dropdown.currentText()
        caption = 'Chose a .qml-legend file for the "{}"-layer...'.format(
            layer)
        filename = QtGui.QFileDialog.getOpenFileName(iface.mainWindow(),
                                                     caption=caption,
                                                     filter='*.qml')
        self.color_entry_manager.read_color_map_from_qml(filename)
        self.update_color_values()
        activeextensions = extensions.by_name(layer)
        try:
            activeextensions[0].colortable = filename
        except:
            pass

    def save_color_map(self):
        """
        Save the currently open color-map as a qml-file into the project folder.
        :return:
        :rtype:
        """
        from mole import extensions

        layer = iface.activeLayer()
        selected_layer = self.layers_dropdown.currentText()
        if layer.name() != selected_layer:
            layer = layer_interaction.find_layer_by_name(selected_layer)

        out_path = os.path.dirname(layer.publicSource())
        out_path = os.path.join(out_path, layer.name() + '.qml')
        self.update_color_values()
        entry_written = self.color_entry_manager.write_color_map_as_qml(
            layer.name(), out_path)
        if entry_written:
            self.message_label.setStyleSheet(_fromUtf8("color: green;"))
            self.message_label.setText(
                'Success - Legend was successfully written to \n\t"{}".'.
                format(out_path))
        else:
            self.message_label.setStyleSheet(_fromUtf8("color: red;"))
            self.message_label.setText(
                'Failure - Could not write legend to to \n\t"{}".'.format(
                    out_path))
        activeextensions = extensions.by_layername(layer.name())
        try:
            activeextensions[0].colortable = out_path
        except:
            pass

    def refresh_layer_list(self):
        from mole import extensions
        """
        Update the color-pickers layer-dropdown with a list of the currently visible .tif-files
        :return:
        :rtype:
        """
        legend.nodeExitSolo()
        dropdown = self.layers_dropdown
        dropdown.clear()
        raster_list = layer_interaction.get_raster_layer_list(
            iface, visibility='visible')
        layer = None
        for layer in raster_list:
            source = layer.publicSource()
            dropdown.addItem(layer.name())
            self.color_entry_manager.add_layer(layer.name())
            ext = extensions.by_layername(layer.name())
            if ext == []:
                ltu_file = os.path.dirname(layer.publicSource())
                ltu_file = os.path.join(ltu_file, layer.name() + '.qml')
            else:
                ext = ext[0]
                ltu_file = ext.colortable
                #print ext.colortable
            #print ltu_file
            if ltu_file != None:
                if os.path.isfile(os.path.join(ltu_file)):
                    self.color_entry_manager.read_color_map_from_qml(
                        os.path.join(ltu_file))
        oeq_global.QeQ_current_work_layer = layer_interaction.find_layer_by_name(
            self.layers_dropdown.currentText())
        if oeq_global.QeQ_current_work_layer != None:
            #print config.open_layers_layer_name
            legend.nodeInitSolo([
                config.investigation_shape_layer_name,
                config.building_outline_layer_name,
                oeq_global.QeQ_current_work_layer.name(),
                config.open_layers_layer_name
            ])
        #oeq_global.QeQ_current_work_layer = layer
        #layer_interaction.move_layer_to_position(iface, layer, 0)

    def setup_legend(self):
        legend.nodeExitSolo()
        oeq_global.QeQ_current_work_layer = layer_interaction.find_layer_by_name(
            self.layers_dropdown.currentText())
        if oeq_global.QeQ_current_work_layer != None:
            #print config.open_layers_layer_name
            legend.nodeInitSolo([
                config.investigation_shape_layer_name,
                config.building_outline_layer_name,
                oeq_global.QeQ_current_work_layer.name(),
                config.open_layers_layer_name
            ])

    def show(self):
        """
        Update the dropdown menu and hide all .tif-layers except for the one which is currently selected.
        :return:
        :rtype:
        """
        self.refresh_layer_list()
        QtGui.QDialog.show(self)
        self.update_color_values()

    def exec_(self):
        """
        Call the super exec_ method and make the .tif-layers visible again,
        which were hidden during the color-picking-process.
        :return:
        :rtype:
        """
        self.show()
        QtGui.QDialog.exec_(self)
        for i in range(self.layers_dropdown.count()):
            layer_name = self.layers_dropdown.itemText(i)
            layer_interaction.unhide_or_remove_layer(layer_name, 'unhide',
                                                     iface)
Exemplo n.º 14
0
class ColorPicker_dialog(QtGui.QDialog, Ui_color_picker_dialog):

    def __init__(self):
        QtGui.QDialog.__init__(self, None, QtCore.Qt.WindowStaysOnTopHint)
        self.setupUi(self)
        self.color_entry_manager = ColorEntryManager()
        self.recent_layer = ''
        self.layers_dropdown.currentIndexChanged.connect(self.update_color_values)
        self.destroyed.connect(legend.nodeExitSolo)
        # set the table model
        self.header = ['Color value', 'Parameter Name', 'From', 'To', '']
        self.color_entry_manager.add_layer('dummy')
        self.color_entry_manager.add_color_value_quadruple_to_layer(('RGBa(0, 0, 0, 0)', ' ', 0, 0), 'dummy')
        self.dummy_model = QColorTableModel(self.color_entry_manager.layer_values_map['dummy'], self.header, self)
        self.color_table_view.setModel(self.dummy_model)
        self.setup_table()

        # connect the signals
        self.color_table_view.clicked.connect(self.remove_entry)
        self.refresh_layers_dropdown.clicked.connect(self.refresh_layer_list)
        self.layers_dropdown.currentIndexChanged.connect(self.setup_legend)

        role = QtGui.QDialogButtonBox.ActionRole
        load_button = self.buttonBox.addButton('Load legend...', role)
        load_button.clicked.connect(self.load_color_map)
        save_button = self.buttonBox.addButton('Save legend', role)
        save_button.clicked.connect(self.save_color_map)

    def setup_table(self):
        """
        Set the basic attributes for the color-table view
        :return:
        :rtype:
        """
        for i in range(len(self.header)):
            self.color_table_view.setItemDelegateForColumn(i, QColorTableDelegate(self))

        self.color_table_view.setColumnWidth(0, 205)
        self.color_table_view.setColumnWidth(1, 205)
        self.color_table_view.setColumnWidth(2, 75)
        self.color_table_view.setColumnWidth(3, 75)
        self.color_table_view.setColumnWidth(4, 35)
        self.color_table_view.setShowGrid(False)
        self.color_table_view.setSortingEnabled(True)
        self.color_table_view.sortByColumn(2)

        # set horizontal header properties
        hor_header = self.color_table_view.horizontalHeader()
        hor_header.setStretchLastSection(True)
        hor_header.setStyleSheet('QHeaderView::section { '
                         ' border: None;'
                         ' padding: 3px;'
                         ' font-size: 13px;'
                         ' font-weight: bold;'
                         ' margin: 5px;'
                         ' background-color: rgb(237, 237, 237);'
                         '}'
                         )
        self.color_table_view.setHorizontalHeader(hor_header)

        # set row height
        ver_header = self.color_table_view.verticalHeader()
        ver_header.setDefaultSectionSize(34)
        ver_header.setStyleSheet('QHeaderView::section { '
                         ' border: None;'
                         ' padding: 3px;'
                         ' font-size: 13px;'
                         ' margin: 5px;'
                         ' margin-right: 12px;'
                         ' background-color: rgb(237, 237, 237);'
                         '}'
                         )
        self.color_table_view.setVerticalHeader(ver_header)

    def remove_entry(self, model_index):
        """
        Remove the entry from the color-entry-manager
        :param model_index: The QModelIndex of the clicked element
        :type model_index: QModelIndex
        :return:
        :rtype:
        """
        column = model_index.column()
        if column == 4:
            row = model_index.row()
            model = model_index.model()
            color_key = model.in_data.keys()[row]
            layer = self.layers_dropdown.currentText()
            self.color_entry_manager.remove_color_entry_from_layer(color_key, layer)
            color_map = self.color_entry_manager.layer_values_map[layer]
            if not color_map:
                color_map = self.color_entry_manager.layer_values_map['dummy']
            tm = QColorTableModel(color_map, self.header, self)
            self.color_table_view.setModel(tm)

    def add_color(self, color):
        """
        Insert a new color (and the values associated to it, if any), into the color-table.
        :param color: The color in RGBa
        :type color: QColor
        :return:
        :rtype:
        """
        from mole import extensions

        layer = self.layers_dropdown.currentText()
        self.recent_layer = layer
        color_map = self.color_entry_manager.layer_values_map[layer]
        model = QColorTableModel(color_map, self.header, self)
        self.color_table_view.setModel(model)

        color_key = 'RGBa({}, {}, {}, {})'.format(color.red(), color.green(), color.blue(), color.alpha())
        if color_map.has_key(color_key):
            self.message_label.setStyleSheet(_fromUtf8("color: red;"))
            self.message_label.setText('Attention: Color {} is defined already.'.format(color_key))

        else:
            self.message_label.clear()
            #ToDo change to use the correct para-name instead
            #parameter_name = str(self.layers_dropdown.currentText())[:10]
            parameter_name = extensions.by_layername(layer)
            if not parameter_name: return None
            parameter_name = parameter_name[0].field_id
            self.color_entry_manager.add_color_value_quadruple_to_layer([color_key, parameter_name, 0, 0], layer)
            color_map = self.color_entry_manager.layer_values_map[layer]
            model = QColorTableModel(color_map, self.header, self)
            self.color_table_view.setModel(model)
        self.raise_()

    def update_color_values(self):
        """
        Change the color-table view to display the color-map of the currently selected layer.
        :return:
        :rtype:
        """
        layer = self.layers_dropdown.currentText()
        color_map = {}
        if layer in self.color_entry_manager.layer_values_map:
            color_map = self.color_entry_manager.layer_values_map[layer]
            table_model = QColorTableModel(color_map, self.header, self)
        if not color_map:
            table_model = self.dummy_model
        self.color_table_view.setModel(table_model)
        self.recent_layer = layer

        try:
            wms_layer = legend.nodeByName(layer)[0].layer()
            url = wms_layer.legendUrl()
            self.legend_view.load(QtCore.QUrl(url))
        except (TypeError, AttributeError) as NoneTypeError:
            pass

    def check_character_constraint(self, parameter_name):
        """
        Check if the parameter-name has a length of ten characters at most,
        since layer-attributes are limited to 10 characters.
        :param parameter_name:
        :type parameter_name:
        :return:
        :rtype:
        """
        if len(parameter_name) == 10 and parameter_name != 'Parameter ':
            self.message_label.setStyleSheet(_fromUtf8("color: red;"))
            self.message_label.setText('Warning: A maximum of 10 characters is allowed as a parameter name!')
        else:
            self.message_label.clear()

    def load_color_map(self):
        """
        Load a color-map for the currently opened layer.
        :return:
        :rtype:
        """
        from mole import extensions

        layer = self.layers_dropdown.currentText()
        caption = 'Chose a .qml-legend file for the "{}"-layer...'.format(layer)
        filename = QtGui.QFileDialog.getOpenFileName(iface.mainWindow(), caption=caption, filter='*.qml')
        self.color_entry_manager.read_color_map_from_qml(filename)
        self.update_color_values()
        activeextensions = extensions.by_name(layer)
        try:
            activeextensions[0].colortable = filename
        except:
            pass

    def save_color_map(self):
        """
        Save the currently open color-map as a qml-file into the project folder.
        :return:
        :rtype:
        """
        from mole import extensions

        layer = iface.activeLayer()
        selected_layer = self.layers_dropdown.currentText()
        if layer.name() != selected_layer:
            layer = layer_interaction.find_layer_by_name(selected_layer)

        out_path = os.path.dirname(layer.publicSource())
        out_path = os.path.join(out_path, layer.name() + '.qml')
        self.update_color_values()
        entry_written = self.color_entry_manager.write_color_map_as_qml(layer.name(), out_path)
        if entry_written:
            self.message_label.setStyleSheet(_fromUtf8("color: green;"))
            self.message_label.setText('Success - Legend was successfully written to \n\t"{}".'.format(out_path))
        else:
            self.message_label.setStyleSheet(_fromUtf8("color: red;"))
            self.message_label.setText('Failure - Could not write legend to to \n\t"{}".'.format(out_path))
        activeextensions = extensions.by_layername(layer.name())
        try:
            activeextensions[0].colortable = out_path
        except:
            pass

    def refresh_layer_list(self):
        from mole import extensions
        """
        Update the color-pickers layer-dropdown with a list of the currently visible .tif-files
        :return:
        :rtype:
        """
        legend.nodeExitSolo()
        dropdown = self.layers_dropdown
        dropdown.clear()
        raster_list = layer_interaction.get_raster_layer_list(iface, visibility='visible')
        layer = None
        for layer in raster_list:
            source = layer.publicSource()
            dropdown.addItem(layer.name())
            self.color_entry_manager.add_layer(layer.name())
            ext = extensions.by_layername(layer.name())
            if ext == []:
                ltu_file = os.path.dirname(layer.publicSource())
                ltu_file = os.path.join(ltu_file, layer.name() + '.qml')
            else:
                ext = ext[0]
                ltu_file = ext.colortable
                #print ext.colortable
            #print ltu_file
            if ltu_file != None:
                if os.path.isfile(os.path.join(ltu_file)):
                    self.color_entry_manager.read_color_map_from_qml(os.path.join(ltu_file))
        oeq_global.QeQ_current_work_layer = layer_interaction.find_layer_by_name(self.layers_dropdown.currentText())
        if oeq_global.QeQ_current_work_layer != None:
            #print config.open_layers_layer_name
            legend.nodeInitSolo([config.investigation_shape_layer_name, config.building_outline_layer_name, oeq_global.QeQ_current_work_layer.name(), config.open_layers_layer_name])
        #oeq_global.QeQ_current_work_layer = layer
        #layer_interaction.move_layer_to_position(iface, layer, 0)

    def setup_legend(self):
        legend.nodeExitSolo()
        oeq_global.QeQ_current_work_layer = layer_interaction.find_layer_by_name(self.layers_dropdown.currentText())
        if oeq_global.QeQ_current_work_layer != None:
            #print config.open_layers_layer_name
            legend.nodeInitSolo([config.investigation_shape_layer_name, config.building_outline_layer_name, oeq_global.QeQ_current_work_layer.name(), config.open_layers_layer_name])



    def show(self):
        """
        Update the dropdown menu and hide all .tif-layers except for the one which is currently selected.
        :return:
        :rtype:
        """
        self.refresh_layer_list()
        QtGui.QDialog.show(self)
        self.update_color_values()

    def exec_(self):
        """
        Call the super exec_ method and make the .tif-layers visible again,
        which were hidden during the color-picking-process.
        :return:
        :rtype:
        """
        self.show()
        QtGui.QDialog.exec_(self)
        for i in range(self.layers_dropdown.count()):
            layer_name = self.layers_dropdown.itemText(i)
            layer_interaction.unhide_or_remove_layer(layer_name, 'unhide', iface)