# HG changeset patch # User Hasan Yavuz ÖZDERYA # Date 2017-01-01 08:47:49 # Node ID 371135090df8ea9b6868fd898b943b43839295b0 # Parent 3455b67378d92652b06aa6f6dac591a75396eaa9 improved color setting and integrated qcolorwidgets library as external project diff --git a/CMakeLists.txt b/CMakeLists.txt --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -54,8 +54,10 @@ else (BUILD_QWT) endif (QWT_USE_STATIC) endif (BUILD_QWT) +include(BuildQColorWidgets) + # includes -include_directories("./src" ${QWT_INCLUDE_DIR}) +include_directories("./src" ${QWT_INCLUDE_DIR} ${QCW_INCLUDE_DIR}) # wrap UI and resource files qt5_wrap_ui(UI_FILES @@ -123,7 +125,7 @@ add_executable(${PROGRAM_NAME} WIN32 ) # Use the Widgets module from Qt 5. -target_link_libraries(${PROGRAM_NAME} ${QWT_LIBRARY}) +target_link_libraries(${PROGRAM_NAME} ${QWT_LIBRARY} ${QCW_LIBRARY}) qt5_use_modules(${PROGRAM_NAME} Widgets SerialPort) if (BUILD_QWT) diff --git a/cmake/modules/BuildQColorWidgets.cmake b/cmake/modules/BuildQColorWidgets.cmake new file mode 100644 --- /dev/null +++ b/cmake/modules/BuildQColorWidgets.cmake @@ -0,0 +1,29 @@ +# +# Copyright © 2017 Hasan Yavuz Özderya +# +# This file is part of serialplot. +# +# serialplot 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. +# +# serialplot 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 serialplot. If not, see . +# + +include(ExternalProject) + +ExternalProject_Add(QCW + PREFIX qcw + GIT_REPOSITORY https://github.com/mbasaglia/Qt-Color-Widgets + INSTALL_COMMAND "") + +ExternalProject_Get_Property(QCW binary_dir source_dir) +set(QCW_LIBRARY ${binary_dir}/libColorWidgets-qt5.a) +set(QCW_INCLUDE_DIR ${source_dir}/include) diff --git a/cmake/modules/BuildQwt.cmake b/cmake/modules/BuildQwt.cmake --- a/cmake/modules/BuildQwt.cmake +++ b/cmake/modules/BuildQwt.cmake @@ -28,7 +28,8 @@ ExternalProject_Add(QWT -e "s/QWT_CONFIG\\s*\\+=\\s*QwtSvg/#&/" -e "s/QWT_CONFIG\\s*\\+=\\s*QwtOpenGL/#&/" -e "s|QWT_INSTALL_PREFIX\\s*=.*|QWT_INSTALL_PREFIX = |" - /qwtconfig.pri + /qwtconfig.pri + UPDATE_COMMAND "" CONFIGURE_COMMAND qmake /qwt.pro ) diff --git a/serialplot.pro.user b/serialplot.pro.user new file mode 100644 --- /dev/null +++ b/serialplot.pro.user @@ -0,0 +1,259 @@ + + + + + + EnvironmentId + {75be7855-4b40-4a75-b2d8-ba783826b955} + + + ProjectExplorer.Project.ActiveTarget + 0 + + + ProjectExplorer.Project.EditorSettings + + true + false + true + + Cpp + + CppGlobal + + + + QmlJS + + QmlJSGlobal + + + 2 + UTF-8 + false + 4 + false + 80 + true + true + 1 + true + false + 0 + true + 0 + 8 + true + 1 + true + true + true + false + + + + ProjectExplorer.Project.PluginSettings + + + + ProjectExplorer.Project.Target.0 + + Desktop + Desktop + {5b76ccd6-39cb-4bf8-aef1-7eda73647436} + 0 + 0 + 0 + + /home/heyyo/working/qtbuilds/build-serialplot-Desktop-Debug + + + true + qmake + + QtProjectManager.QMakeBuildStep + false + true + + false + false + false + + + true + Make + + Qt4ProjectManager.MakeStep + + false + + + + 2 + Build + + ProjectExplorer.BuildSteps.Build + + + + true + Make + + Qt4ProjectManager.MakeStep + + true + clean + + + 1 + Clean + + ProjectExplorer.BuildSteps.Clean + + 2 + false + + Debug + + Qt4ProjectManager.Qt4BuildConfiguration + 2 + true + + + /home/heyyo/working/qtbuilds/build-serialplot-Desktop-Release + + + true + qmake + + QtProjectManager.QMakeBuildStep + false + true + + false + false + false + + + true + Make + + Qt4ProjectManager.MakeStep + + false + + + + 2 + Build + + ProjectExplorer.BuildSteps.Build + + + + true + Make + + Qt4ProjectManager.MakeStep + + true + clean + + + 1 + Clean + + ProjectExplorer.BuildSteps.Clean + + 2 + false + + Release + + Qt4ProjectManager.Qt4BuildConfiguration + 0 + true + + 2 + + + 0 + Deploy + + ProjectExplorer.BuildSteps.Deploy + + 1 + Deploy locally + + ProjectExplorer.DefaultDeployConfiguration + + 1 + + + + false + false + false + false + true + 0.01 + 10 + true + 1 + 25 + + 1 + true + false + true + valgrind + + 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 + 14 + + 2 + + serialplot + + Qt4ProjectManager.Qt4RunConfiguration:/home/heyyo/working/serialplot/serialplot.pro + + serialplot.pro + false + false + + 3768 + false + true + false + false + true + + 1 + + + + ProjectExplorer.Project.TargetCount + 1 + + + ProjectExplorer.Project.Updater.FileVersion + 18 + + + Version + 18 + + diff --git a/src/channelinfomodel.cpp b/src/channelinfomodel.cpp --- a/src/channelinfomodel.cpp +++ b/src/channelinfomodel.cpp @@ -19,14 +19,10 @@ #include "channelinfomodel.h" -#include -#include - enum ChannelInfoColumn { COLUMN_NAME = 0, COLUMN_VISIBILITY, - COLUMN_COLOR, COLUMN_COUNT }; @@ -69,33 +65,32 @@ Qt::ItemFlags ChannelInfoModel::flags(co { return Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemNeverHasChildren | Qt::ItemIsSelectable; } - else if (index.column() == COLUMN_COLOR) - { - return Qt::ItemIsEnabled | Qt::ItemNeverHasChildren | Qt::ItemIsSelectable; - } return Qt::NoItemFlags; } QVariant ChannelInfoModel::data(const QModelIndex &index, int role) const { + // check index if (index.row() >= (int) _numOfChannels) { return QVariant(); } + // get color + if (role == Qt::ForegroundRole) + { + return infos[index.row()].color; + } + + // get name if (index.column() == COLUMN_NAME) { if (role == Qt::DisplayRole || role == Qt::EditRole) { return QVariant(infos[index.row()].name); } - else if (role == Qt::ForegroundRole) - { - // TODO: support more colors than 8 - return colors[index.row() % 8]; - } - } + } // get visibility else if (index.column() == COLUMN_VISIBILITY) { if (role == Qt::CheckStateRole) @@ -104,13 +99,6 @@ QVariant ChannelInfoModel::data(const QM return visible ? Qt::Checked : Qt::Unchecked; } } - else if (index.column() == COLUMN_COLOR) - { - if (role == Qt::ForegroundRole || role == Qt::BackgroundRole) - { - return colors[index.row() % 8]; - } - } return QVariant(); } @@ -129,10 +117,6 @@ QVariant ChannelInfoModel::headerData(in { return tr("Visible"); } - else if (section == COLUMN_COLOR) - { - return tr("Color"); - } } } else // vertical @@ -148,11 +132,21 @@ QVariant ChannelInfoModel::headerData(in bool ChannelInfoModel::setData(const QModelIndex &index, const QVariant &value, int role) { + // check index if (index.row() >= (int) _numOfChannels) { return false; } + // set color + if (role == Qt::ForegroundRole) + { + infos[index.row()].color = value.value(); + emit dataChanged(index, index, QVector({Qt::ForegroundRole})); + return true; + } + + // set name if (index.column() == COLUMN_NAME) { if (role == Qt::DisplayRole || role == Qt::EditRole) @@ -160,7 +154,7 @@ bool ChannelInfoModel::setData(const QMo infos[index.row()].name = value.toString(); return true; } - } + } // set visibility else if (index.column() == COLUMN_VISIBILITY) { if (role == Qt::CheckStateRole) @@ -195,7 +189,7 @@ void ChannelInfoModel::setNumOfChannels( { for (unsigned ci = _numOfChannels; ci < number; ci++) { - infos.append({QString("Channel %1").arg(ci+1), true}); + infos.append({QString("Channel %1").arg(ci+1), true, colors[ci % 8]}); } } diff --git a/src/channelinfomodel.h b/src/channelinfomodel.h --- a/src/channelinfomodel.h +++ b/src/channelinfomodel.h @@ -21,7 +21,7 @@ #define CHANNELINFOMODEL_H #include -#include +#include class ChannelInfoModel : public QAbstractTableModel { @@ -45,6 +45,7 @@ private: { QString name; bool visibility; + QColor color; }; unsigned _numOfChannels; ///< @note this is not necessarily the length of `infos` diff --git a/src/main.cpp b/src/main.cpp --- a/src/main.cpp +++ b/src/main.cpp @@ -26,7 +26,13 @@ // test code #include +#include +#include #include "channelinfomodel.h" +#include "utils.h" + +#include "color_selector.hpp" +#include "color_dialog.hpp" MainWindow* pMainWindow; @@ -55,16 +61,42 @@ int main(int argc, char *argv[]) w.show(); // test code - ChannelInfoModel cim(10, &a); + ChannelInfoModel cim(2, &a); QTableView tv; + QSpinBox cb; + + QObject::connect(&cb, SELECT::OVERLOAD_OF(&QSpinBox::valueChanged), [&cim](int value) + { + cim.setNumOfChannels(value); + }); tv.setModel(&cim); tv.show(); + // cb.show(); - // cim.setNumOfChannels(3); - // cim.setNumOfChannels(10); - // cim.setNumOfChannels(7); + cim.setNumOfChannels(3); + cim.setNumOfChannels(7); + cim.setNumOfChannels(10); + + + color_widgets::ColorSelector cpicker; + cpicker.setColor(QColor("red")); + cpicker.show(); + + QObject::connect(tv.selectionModel(), &QItemSelectionModel::currentRowChanged, + [&cim, &cpicker](const QModelIndex ¤t, const QModelIndex &previous) + { + cpicker.setColor(cim.data(current, Qt::ForegroundRole).value()); + }); + + QObject::connect(&cpicker, &color_widgets::ColorSelector::colorChanged, + [&cim, &tv](QColor color) + { + auto index = tv.selectionModel()->currentIndex(); + index = index.sibling(index.row(), 0); + cim.setData(index, color, Qt::ForegroundRole); + }); return a.exec(); } diff --git a/src/plotcontrolpanel.ui b/src/plotcontrolpanel.ui --- a/src/plotcontrolpanel.ui +++ b/src/plotcontrolpanel.ui @@ -17,14 +17,25 @@ - - - font-weight: bold; - - - Channel Names: - - + + + + + Channel Names: + + + + + + + + 0 + 0 + + + + + @@ -161,6 +172,14 @@ + + + color_widgets::ColorSelector + QWidget +
color_selector.hpp
+ 1 +
+