Added ColorPaletteModel to keep track of color palettes.

Now Color Widgets requires C++11 to compile
parent 5b0edb14
......@@ -12,6 +12,22 @@ set(CMAKE_INCLUDE_CURRENT_DIR ON)
# Debug
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Wall -pedantic -Werror")
# C++11
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# For some reason the above flags don't really work...
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" OR CMAKE_COMPILER_IS_GNUCXX)
include(CheckCXXCompilerFlag)
check_cxx_compiler_flag(--std=c++${CMAKE_CXX_STANDARD} STD_CXX)
if(STD_CXX)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --std=c++${CMAKE_CXX_STANDARD}")
else()
message(SEND_ERROR "Requires C++${CMAKE_CXX_STANDARD} or better")
endif()
else()
message(WARNING "Unrecognized compiler: ${CMAKE_CXX_COMPILER_ID}, make sure it supports C++${CMAKE_CXX_STANDARD}")
endif()
# Sources
include_directories("${PROJECT_SOURCE_DIR}/include")
......
......@@ -14,6 +14,7 @@
# You should have received a copy of the GNU General Public License
# along with Color Widgets. If not, see <http://www.gnu.org/licenses/>.
CONFIG += c++11
INCLUDEPATH += $$PWD/src $$PWD/include
......@@ -29,6 +30,7 @@ SOURCES += \
$$PWD/src/bound_color_selector.cpp \
$$PWD/src/abstract_widget_list.cpp \
$$PWD/src/color_palette.cpp \
$$PWD/src/color_palette_model.cpp \
$$PWD/src/swatch.cpp
HEADERS += \
......@@ -44,6 +46,7 @@ HEADERS += \
$$PWD/include/abstract_widget_list.hpp \
$$PWD/include/colorwidgets_global.hpp \
$$PWD/include/color_palette.hpp \
$$PWD/include/color_palette_model.hpp \
$$PWD/include/swatch.hpp
FORMS += \
......
......@@ -38,30 +38,7 @@ isEmpty(PREFIX) {
}
target.path = $$PREFIX/lib
headers.path = $$PREFIX/include/QtColorWidgets
headers.files = \
include/AbstractWidgetList \
include/BoundColorSelector \
include/ColorDelegate \
include/ColorDialog \
include/ColorListWidget \
include/ColorPreview \
include/ColorSelector \
include/ColorWheel \
include/GradientSlider \
include/HueSlider \
include/abstract_widget_list.hpp \
include/color_delegate.hpp \
include/color_list_widget.hpp \
include/color_preview.hpp \
include/color_wheel.hpp \
include/bound_color_selector.hpp \
include/color_dialog.hpp \
include/colorwidgets_global.hpp \
include/color_selector.hpp \
include/gradient_slider.hpp \
include/hue_slider.hpp \
include/color_palette.hpp \
include/swatch.hpp
headers.files = $$HEADERS
INSTALLS += target headers
/**
@author Mattia Basaglia
@section License
Copyright (C) 2015 Mattia Basaglia
This software is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This software is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Color Widgets. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef COLOR_WIDGETS_COLOR_PALETTE_MODEL_HPP
#define COLOR_WIDGETS_COLOR_PALETTE_MODEL_HPP
#include <QAbstractListModel>
#include "color_palette.hpp"
namespace color_widgets {
class ColorPaletteModel : public QAbstractListModel
{
Q_OBJECT
/**
* \brief List of directories to be scanned for palette files
*/
Q_PROPERTY(QStringList searchPaths READ searchPaths WRITE setSearchPaths NOTIFY searchPathsChanged)
/**
* \brief Default directory to be used when saving a palette
*/
Q_PROPERTY(QString savePath READ savePath WRITE setSavePath NOTIFY savePathChanged)
/**
* \brief Size of the icon used for the palette previews
*/
Q_PROPERTY(QSize iconSize READ iconSize WRITE setIconSize NOTIFY iconSizeChanged)
public:
ColorPaletteModel();
~ColorPaletteModel();
int rowCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const Q_DECL_OVERRIDE;
QString savePath() const;
QStringList searchPaths() const;
QSize iconSize() const;
/**
* \brief Returns a reference to the first palette with the given name
* \pre hasPalette(name)
*/
const ColorPalette& palette(const QString& name) const;
/**
* \brief Whether a palette with the given name exists in the model
*/
bool hasPalette(const QString& name) const;
/**
* \brief Get the palette at the given index (row)
* \pre 0 <= index < rowCount()
*/
const ColorPalette& palette(int index) const;
/**
* \brief Get the palette at the given index (row)
* \pre 0 <= index < rowCount()
*/
ColorPalette& palette(int index);
public slots:
void setSavePath(const QString& savePath);
void setSearchPaths(const QStringList& searchPaths);
void addSearchPath(const QString& path);
void setIconSize(const QSize& iconSize);
void addPalette(const ColorPalette& palette);
/**
* \brief Load palettes files found in the search paths
*/
void load();
signals:
void savePathChanged(const QString& savePath);
void searchPathsChanged(const QStringList& searchPaths);
void iconSizeChanged(const QSize& iconSize);
private:
class Private;
Private* p;
};
} // namespace color_widgets
#endif // COLOR_WIDGETS_COLOR_PALETTE_MODEL_HPP
......@@ -27,6 +27,7 @@
#include <QTextStream>
#include <QHash>
#include <QPainter>
#include <QFileInfo>
namespace color_widgets {
......@@ -144,11 +145,12 @@ bool ColorPalette::load(const QString& name)
emit fileNameChanged(p->fileName);
p->colors.clear();
p->columns = 0;
p->name = QString();
p->dirty = false;
p->name = QFileInfo(name).baseName();
QFile file(name);
/// \todo emit signals on return false
if ( !file.open(QFile::ReadOnly|QFile::Text) )
return false;
......
/**
@author Mattia Basaglia
@section License
Copyright (C) 2015 Mattia Basaglia
This software is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This software is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Color Widgets. If not, see <http://www.gnu.org/licenses/>.
*/
#include "color_palette_model.hpp"
#include <QDir>
#include <QList>
namespace color_widgets {
class ColorPaletteModel::Private
{
public:
/// \todo Keep sorted by name
QList<ColorPalette> palettes;
QSize icon_size;
QStringList search_paths;
QString save_path;
Private()
: icon_size(32, 32)
{}
bool acceptable(const QModelIndex& index) const
{
return index.row() >= 0 && index.row() <= palettes.count();
}
QList<ColorPalette>::iterator find(const QString& name)
{
return std::find_if(palettes.begin(), palettes.end(),
[&name](const ColorPalette& palette) {
return palette.name() == name;
});
}
};
ColorPaletteModel::ColorPaletteModel()
: p ( new Private )
{}
ColorPaletteModel::~ColorPaletteModel()
{
delete p;
}
int ColorPaletteModel::rowCount(const QModelIndex &) const
{
return p->palettes.count();
}
QVariant ColorPaletteModel::data(const QModelIndex &index, int role) const
{
if ( !p->acceptable(index) )
return QVariant();
const ColorPalette& palette = p->palettes[index.row()];
switch( role )
{
case Qt::DisplayRole:
return palette.name();
case Qt::DecorationRole:
return palette.preview(p->icon_size);
case Qt::ToolTipRole:
return tr("%1 (%2 colors)").arg(palette.name()).arg(palette.count());
}
return QVariant();
}
QSize ColorPaletteModel::iconSize() const
{
return p->icon_size;
}
void ColorPaletteModel::setIconSize(const QSize& iconSize)
{
if ( p->icon_size != iconSize )
emit iconSizeChanged( p->icon_size = iconSize );
}
QString ColorPaletteModel::savePath() const
{
return p->save_path;
}
QStringList ColorPaletteModel::searchPaths() const
{
return p->search_paths;
}
void ColorPaletteModel::setSavePath(const QString& savePath)
{
if ( p->save_path != savePath )
emit savePathChanged( p->save_path = savePath );
}
void ColorPaletteModel::setSearchPaths(const QStringList& searchPaths)
{
if ( p->search_paths != searchPaths )
emit searchPathsChanged( p->search_paths = searchPaths );
}
void ColorPaletteModel::addSearchPath(const QString& path)
{
/// \todo Should compare absolute canonical paths,
/// and these checks should also be made in setSearchPaths
if ( !p->search_paths.contains(path) )
{
p->search_paths.push_back(path);
emit searchPathsChanged( p->search_paths );
}
}
void ColorPaletteModel::load()
{
beginResetModel();
p->palettes.clear();
QStringList filters;
filters << "*.gpl";
for ( const QString& directory_name : p->search_paths )
{
QDir directory(directory_name);
directory.setNameFilters(filters);
directory.setFilter(QDir::Files|QDir::Readable);
directory.setSorting(QDir::Name);
for ( const QFileInfo& file : directory.entryInfoList() )
{
ColorPalette palette;
if ( palette.load(file.absoluteFilePath()) )
{
p->palettes.push_back(palette);
}
}
}
endResetModel();
}
void ColorPaletteModel::addPalette(const ColorPalette& palette)
{
p->palettes.push_back(palette);
if ( p->palettes.back().name().isEmpty() )
p->palettes.back().setName(tr("Unnamed"));
}
bool ColorPaletteModel::hasPalette(const QString& name) const
{
return p->find(name) != p->palettes.end();
}
const ColorPalette& ColorPaletteModel::palette(const QString& name) const
{
return *p->find(name);
}
const ColorPalette& ColorPaletteModel::palette(int index) const
{
return p->palettes[index];
}
ColorPalette& ColorPaletteModel::palette(int index)
{
return p->palettes[index];
}
} // namespace color_widgets
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment