Commit 16004d22 authored by LEFEBVREJP email's avatar LEFEBVREJP email
Browse files

Merge branch 'widgets' into 'master'

Widgets

See merge request !41
parents 3223d275 5af228b8
Pipeline #13850 passed with stages
in 11 minutes and 58 seconds
......@@ -12,6 +12,7 @@ IF (NOT TRIBITS_PROCESSING_PACKAGE)
PROJECT(${PROJECT_NAME} NONE)
# enable C++11 by default
SET(radix_ENABLE_CXX11 ON CACHE BOOL "Compile using the C++11 standard" FORCE)
SET(USE_QT5 TRUE)
# disable generating HTML dependencies webpage and xml files
SET(${PROJECT_NAME}_DEPS_XML_OUTPUT_FILE OFF CACHE BOOL "")
......
......@@ -55,6 +55,10 @@
SET(radix_TPLS_FINDMODS_CLASSIFICATIONS
LAPACK "${${PROJECT_NAME}_TRIBITS_DIR}/common_tpls/" PT
Qt5Core "cmake/TPLs/" ST
Qt5Gui "cmake/TPLs/" ST
Qt5Widgets "cmake/TPLs/" ST
VTK "cmake/TPLs/" ST
)
##---------------------------------------------------------------------------##
......
IF(COMMAND QT_PRL_ADDITIONS)
RETURN()
ENDIF()
FUNCTION(QT_PRL_ADDITIONS LIB_NAME LIB_DIR ADDITIONS_OUT)
#platform dependent component configuration name
IF(CMAKE_SYSTEM_NAME MATCHES "Windows")
SET(component_pkg ${component_lib_dir}/${component}.prl)
ELSEIF( EXISTS "${component_lib_dir}/${component}.prl")
SET(component_pkg ${component_lib_dir}/${component}.prl)
ELSE()
SET(component_pkg ${component_lib_dir}/lib${component}.prl)
ENDIF()
MESSAGE(STATUS "Searching for component: ${component_pkg}")
#Check if this componet configuration exists
IF( EXISTS ${component_pkg} )
MESSAGE("-- Found component(${component}) configuration: ${component_pkg}")
#read the component description file
FILE(STRINGS ${component_pkg} file_lines)
SET(FRAMEWORK FALSE)
#loop over each line
FOREACH(line ${file_lines})
#only pay attention to library line
IF(line MATCHES "QMAKE_PRL_LIBS")
STRING(REGEX REPLACE "^.*=" "" line_pruned ${line})
SPLIT( "${line_pruned}" " +" field_list)
#
# Process field list to remove qt libraries
#
SET(processed_field_list ${field_list})
IF(CMAKE_SYSTEM_NAME STREQUAL "Windows" AND NOT CMAKE_C_COMPILER_ID STREQUAL "GNU")
LIST(APPEND ADDITIONAL_LIBS ${field_list})
ELSE()
FOREACH(field ${field_list})
IF( field MATCHES "-l" )
STRING(REGEX REPLACE "-l" "" field ${field})
# Ignore internal qt module dependencies
IF(NOT field MATCHES "^Qt")
LIST(APPEND ADDITIONAL_LIBS ${field})
ENDIF()
ELSEIF( field MATCHES "-F" )
# check for Darwin frameworks directory "-F/opt/local/..."
# ignore internal qt module directories
IF(NOT field MATCHES "QT")
LIST(APPEND ADDITIONAL_LIBS "${field}")
ENDIF()
ELSEIF( field MATCHES "-framework" )
# check for Darwin frameworks "-framework ApplicationServices"
SET(FRAMEWORK TRUE)
ELSEIF( FRAMEWORK ) # check for Darwin frameworks
# Ignore internal qt module dependencies
IF(NOT field MATCHES "^Qt")
LIST(APPEND ADDITIONAL_LIBS "-framework ${field}")
ENDIF()
SET(FRAMEWORK FALSE)
ENDIF()
ENDFOREACH()
ENDIF()
BREAK()
ENDIF()
ENDFOREACH()
ELSE()
MESSAGE("-- Did not find component(${component})")
MESSAGE("-- System libraries may not be included correctly!")
MESSAGE("-- Resulting in undefined symbols at link time.")
ENDIF()
SET(ADDITIONAL_LIBS ${ADDITIONAL_LIBS} PARENT_SCOPE)
ENDFUNCTION()
INCLUDE(QtPrlAdditions)
IF(Qt5Core_LIBRARY_DIRS)
SET(Qt5Core_LIBRARY_DIR ${Qt5Core_LIBRARY_DIRS})
ENDIF()
IF(TPL_Qt5Core_LIBRARY_DIRS)
SET(Qt5Core_LIBRARY_DIR ${TPL_Qt5Core_LIBRARY_DIRS})
ENDIF()
IF(Qt5Core_INCLUDE_DIRS)
SET(Qt5Core_INCLUDE_DIR ${Qt5Core_INCLUDE_DIRS})
ENDIF()
IF(TPL_Qt5Core_INCLUDE_DIRS)
SET(Qt5Core_INCLUDE_DIR ${TPL_Qt5Core_INCLUDE_DIRS})
ENDIF()
IF(TPL_Qt5Core_LIBRARIES)
SET(Qt5Core_LIBRARIES ${TPL_Qt5Core_LIBRARIES})
ENDIF()
IF(NOT Qt5Core_REQUIRED_VERSION)
SET(Qt5Core_REQUIRED_VERSION 5.1.0)
ENDIF()
MESSAGE("REQUIRED Qt5Core version: ${Qt5Core_REQUIRED_VERSION}")
FIND_PACKAGE(Qt5Core "${Qt5Core_REQUIRED_VERSION}")
if(NOT Qt5Core_FOUND)
message(" ____")
message(" __,-~~/~ `---.")
message(" _/_,---( , )")
message(" __ / < / ) \\___")
message("- ------===;;;'====------------------===;;;===----- - -")
message(" \\/ ~\"~\"~\"~\"~\"~\\~\"~)~\"/")
message(" (_ ( \\ ( > \\)")
message(" \\_( _ < >_>'")
message(" ~ `-i' ::>|--\"")
message(" I;|.|.|")
message(" <|i::|i|`.")
message(" (` ^'`-' )")
MESSAGE(FATAL_ERROR "Couldn't find Qt5Core. This causes explosions.")
endif()
#
# Bug fix for Qt5 cmake configuration. They do not cache their variables for project use
#
SET(Qt5Core_QMAKE_EXECUTABLE ${Qt5Core_QMAKE_EXECUTABLE} CACHE STRING "")
SET(Qt5Core_MOC_EXECUTABLE ${Qt5Core_MOC_EXECUTABLE} CACHE STRING "")
SET(Qt5Core_RCC_EXECUTABLE ${Qt5Core_RCC_EXECUTABLE} CACHE STRING "")
MESSAGE(STATUS "QMAKE_EXECUTABLE: '${Qt5Core_QMAKE_EXECUTABLE}'")
MESSAGE(STATUS "MOC_EXECUTABLE: '${Qt5Core_MOC_EXECUTABLE}'")
MESSAGE(STATUS "RCC_EXECUTABLE: '${Qt5Core_RCC_EXECUTABLE}'")
#
# Extra work to get the locations correct
GET_TARGET_PROPERTY(Qt5Core_location Qt5::Core LOCATION)
GET_FILENAME_COMPONENT(Qt5Core_LIBRARY_DIRS "${Qt5Core_location}" DIRECTORY CACHE)
MESSAGE(STATUS "Qt5Core_LIBRARY_DIRS=${Qt5Core_LIBRARY_DIRS}")
#
# Get the root of this Qt installation for reference
IF(Qt5Core_LIBRARY_DIRS MATCHES ".*framework/?$")
GET_FILENAME_COMPONENT(Qt5_INSTALL_PREFIX "${Qt5Core_LIBRARY_DIRS}/../../" ABSOLUTE CACHE)
ELSE()
GET_FILENAME_COMPONENT(Qt5_INSTALL_PREFIX "${Qt5Core_LIBRARY_DIRS}/../" ABSOLUTE CACHE)
ENDIF()
MESSAGE(STATUS "Qt5_INSTALL_PREFIX: ${Qt5_INSTALL_PREFIX}")
SET(Qt5_PLUGINS_DIR ${Qt5_INSTALL_PREFIX}/plugins CACHE STRING "Location of Qt5 plugins")
MESSAGE(STATUS "Qt5_PLUGINS_DIR: ${Qt5_PLUGINS_DIR}")
IF(NOT(Qt5Core_INCLUDE_DIRS))
SET(Qt5Core_INCLUDE_DIRS ${Qt5Core_INCLUDE_DIRS})
ENDIF()
IF(NOT(Qt5Core_LIBRARY_DIRS))
SET(Qt5Core_LIBRARY_DIRS ${Qt5Core_LIBRARY_DIRS})
ENDIF()
IF(NOT TPL_Qt5Core_INCLUDE_DIRS )
SET(TPL_Qt5Core_INCLUDE_DIRS ${Qt5Core_INCLUDE_DIRS})
ENDIF()
IF(NOT TPL_Qt5Core_LIBRARY_DIRS )
SET(TPL_Qt5Core_LIBRARY_DIRS ${Qt5Core_LIBRARY_DIRS})
ENDIF()
IF(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
SET(LIB_NAME QtCore)
ELSE()
SET(LIB_NAME Qt5Core)
ENDIF()
SET(component ${LIB_NAME})
SET(component_lib_dir "${Qt5Core_LIBRARY_DIRS}")
QT_PRL_ADDITIONS(${component} ${component_lib_dir} ADDITIONAL_LIBS)
#remove duplicate entries list
IF(ADDITIONAL_LIBS)
LIST(REMOVE_DUPLICATES ADDITIONAL_LIBS)
MESSAGE(STATUS "Found additional libraries: ${ADDITIONAL_LIBS}")
ENDIF()
SET(TPL_Qt5Core_LIBRARIES Qt5::Core ${ADDITIONAL_LIBS} CACHE STRING "")
SET(TPL_Qt5Core_INCLUDE_DIRS ${TPL_Qt5Core_INCLUDE_DIRS} CACHE STRING "")
INCLUDE(QtPrlAdditions)
IF(Qt5Gui_LIBRARY_DIRS)
SET(Qt5Gui_LIBRARY_DIR ${Qt5Gui_LIBRARY_DIRS})
ENDIF()
IF(TPL_Qt5Gui_LIBRARY_DIRS)
SET(Qt5Gui_LIBRARY_DIR ${TPL_Qt5Gui_LIBRARY_DIRS})
ENDIF()
IF(Qt5Gui_INCLUDE_DIRS)
SET(Qt5Gui_INCLUDE_DIR ${Qt5Gui_INCLUDE_DIRS})
ENDIF()
IF(TPL_Qt5Gui_INCLUDE_DIRS)
SET(Qt5Gui_INCLUDE_DIR ${TPL_Qt5Gui_INCLUDE_DIRS})
ENDIF()
IF(TPL_Qt5Gui_LIBRARIES)
SET(Qt5Gui_LIBRARIES ${TPL_Qt5Gui_LIBRARIES})
ENDIF()
IF(NOT Qt5Gui_REQUIRED_VERSION)
SET(Qt5Gui_REQUIRED_VERSION 5.1.0)
ENDIF()
MESSAGE("REQUIRED Qt5Gui version: ${Qt5Gui_REQUIRED_VERSION}")
FIND_PACKAGE(Qt5Gui "${Qt5Gui_REQUIRED_VERSION}")
if(NOT Qt5Gui_FOUND)
message(" ____")
message(" __,-~~/~ `---.")
message(" _/_,---( , )")
message(" __ / < / ) \\___")
message("- ------===;;;'====------------------===;;;===----- - -")
message(" \\/ ~\"~\"~\"~\"~\"~\\~\"~)~\"/")
message(" (_ ( \\ ( > \\)")
message(" \\_( _ < >_>'")
message(" ~ `-i' ::>|--\"")
message(" I;|.|.|")
message(" <|i::|i|`.")
message(" (` ^'`-' )")
MESSAGE(FATAL_ERROR "Couldn't find Qt5Gui. This causes explosions.")
endif()
#
# Bug fix for Qt5 cmake configuration. They do not cache their variables for project use
#
SET(Qt5Core_QMAKE_EXECUTABLE ${Qt5Core_QMAKE_EXECUTABLE} CACHE STRING "")
SET(Qt5Core_MOC_EXECUTABLE ${Qt5Core_MOC_EXECUTABLE} CACHE STRING "")
SET(Qt5Core_RCC_EXECUTABLE ${Qt5Core_RCC_EXECUTABLE} CACHE STRING "")
#
# Extra work to get the location correct
GET_TARGET_PROPERTY(Qt5Gui_location Qt5::Gui LOCATION)
GET_FILENAME_COMPONENT(Qt5Gui_LIBRARY_DIRS "${Qt5Gui_location}" DIRECTORY CACHE)
IF(NOT(Qt5Gui_INCLUDE_DIRS))
SET(Qt5Gui_INCLUDE_DIRS ${Qt5Gui_INCLUDE_DIRS})
ENDIF()
IF(NOT(Qt5Gui_LIBRARY_DIRS))
SET(Qt5Gui_LIBRARY_DIRS ${Qt5Gui_LIBRARY_DIRS})
ENDIF()
IF(NOT(TPL_Qt5Gui_INCLUDE_DIRS))
SET(TPL_Qt5Gui_INCLUDE_DIRS ${Qt5Gui_INCLUDE_DIRS})
ENDIF()
IF(NOT(TPL_Qt5Gui_LIBRARY_DIRS))
SET(TPL_Qt5Gui_LIBRARY_DIRS ${Qt5Gui_LIBRARY_DIRS})
ENDIF()
IF(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
SET(LIB_NAME QtGui)
ELSE()
SET(LIB_NAME Qt5Gui)
ENDIF()
SET(component ${LIB_NAME})
SET(component_lib_dir "${Qt5Gui_LIBRARY_DIRS}")
QT_PRL_ADDITIONS(${component} ${component_lib_dir} ADDITIONAL_LIBS)
#remove duplicate entries list
IF(ADDITIONAL_LIBS)
LIST(REMOVE_DUPLICATES ADDITIONAL_LIBS)
MESSAGE(STATUS "Found additional libraries: ${ADDITIONAL_LIBS}")
ENDIF()
SET(TPL_Qt5Gui_LIBRARIES Qt5::Gui ${ADDITIONAL_LIBS} CACHE STRING "")
SET(TPL_Qt5Gui_INCLUDE_DIRS ${TPL_Qt5Gui_INCLUDE_DIRS} CACHE STRING "")
INCLUDE(QtPrlAdditions)
IF(Qt5Widgets_LIBRARY_DIRS)
SET(Qt5Widgets_LIBRARY_DIR ${Qt5Widgets_LIBRARY_DIRS})
ENDIF()
IF(TPL_Qt5Widgets_LIBRARY_DIRS)
SET(Qt5Widgets_LIBRARY_DIR ${TPL_Qt5Widgets_LIBRARY_DIRS})
ENDIF()
IF(Qt5Widgets_INCLUDE_DIRS)
SET(Qt5Widgets_INCLUDE_DIR ${Qt5Widgets_INCLUDE_DIRS})
ENDIF()
IF(TPL_Qt5Widgets_INCLUDE_DIRS)
SET(Qt5Widgets_INCLUDE_DIR ${TPL_Qt5Widgets_INCLUDE_DIRS})
ENDIF()
IF(TPL_Qt5Widgets_LIBRARIES)
SET(Qt5Widgets_LIBRARIES ${TPL_Qt5Widgets_LIBRARIES})
ENDIF()
IF(NOT Qt5Widgets_REQUIRED_VERSION)
SET(Qt5Widgets_REQUIRED_VERSION 5.1.0)
ENDIF()
MESSAGE("REQUIRED Qt5Widgets version: ${Qt5Widgets_REQUIRED_VERSION}")
FIND_PACKAGE(Qt5Widgets "${Qt5Widgets_REQUIRED_VERSION}")
if(NOT Qt5Widgets_FOUND)
message(" ____")
message(" __,-~~/~ `---.")
message(" _/_,---( , )")
message(" __ / < / ) \\___")
message("- ------===;;;'====------------------===;;;===----- - -")
message(" \\/ ~\"~\"~\"~\"~\"~\\~\"~)~\"/")
message(" (_ ( \\ ( > \\)")
message(" \\_( _ < >_>'")
message(" ~ `-i' ::>|--\"")
message(" I;|.|.|")
message(" <|i::|i|`.")
message(" (` ^'\"`-' \")")
MESSAGE(FATAL_ERROR "Couldn't find Qt5Widgets. This causes explosions.")
endif()
#
# Bug fix for Qt5 cmake configuration. They do not cache their variables for project use
#
SET(Qt5Core_QMAKE_EXECUTABLE ${Qt5Core_QMAKE_EXECUTABLE} CACHE STRING "")
SET(Qt5Core_MOC_EXECUTABLE ${Qt5Core_MOC_EXECUTABLE} CACHE STRING "")
SET(Qt5Core_RCC_EXECUTABLE ${Qt5Core_RCC_EXECUTABLE} CACHE STRING "")
SET(Qt5Widgets_UIC_EXECUTABLE ${Qt5Widgets_UIC_EXECUTABLE} CACHE STRING "")
MESSAGE(STATUS "UIC_EXECUTABLE: ${Qt5Widgets_UIC_EXECUTABLE}")
#
# Extra work to get the location correct
GET_TARGET_PROPERTY(Qt5Widgets_location Qt5::Widgets LOCATION)
GET_FILENAME_COMPONENT(Qt5Widgets_LIBRARY_DIRS "${Qt5Widgets_location}" DIRECTORY CACHE)
IF(NOT(Qt5Widgets_INCLUDE_DIRS))
SET(Qt5Widgets_INCLUDE_DIRS ${Qt5Widgets_INCLUDE_DIRS})
ENDIF()
IF(NOT(Qt5Widgets_LIBRARY_DIRS))
SET(Qt5Widgets_LIBRARY_DIRS ${Qt5Widgets_LIBRARY_DIRS})
ENDIF()
IF(NOT(TPL_Qt5Widgets_INCLUDE_DIRS))
SET(TPL_Qt5Widgets_INCLUDE_DIRS ${Qt5Widgets_INCLUDE_DIRS})
ENDIF()
IF(NOT(TPL_Qt5Widgets_LIBRARY_DIRS))
SET(TPL_Qt5Widgets_LIBRARY_DIRS ${Qt5Widgets_LIBRARY_DIRS})
ENDIF()
IF(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
SET(LIB_NAME QtWidgets)
ELSE()
SET(LIB_NAME Qt5Widgets)
ENDIF()
SET(component ${LIB_NAME})
SET(component_lib_dir "${Qt5Widgets_LIBRARY_DIRS}")
QT_PRL_ADDITIONS(${component} ${component_lib_dir} ADDITIONAL_LIBS)
#remove duplicate entries list
IF(ADDITIONAL_LIBS)
LIST(REMOVE_DUPLICATES ADDITIONAL_LIBS)
MESSAGE(STATUS "Found additional libraries: ${ADDITIONAL_LIBS}")
ENDIF()
SET(TPL_Qt5Widgets_LIBRARIES Qt5::Widgets ${ADDITIONAL_LIBS} CACHE STRING "")
SET(TPL_Qt5Widgets_INCLUDE_DIRS ${TPL_Qt5Widgets_INCLUDE_DIRS} CACHE STRING "")
IF(NOT VTK_REQUIRED_VERSION)
SET(VTK_REQUIRED_VERSION 8.1.0)
ENDIF()
IF(VTK_LIBRARY_DIRS)
SET(VTK_LIBRARY_DIR ${VTK_LIBRARY_DIRS})
ENDIF()
IF(TPL_VTK_LIBRARY_DIRS)
SET(VTK_LIBRARY_DIR ${TPL_VTK_LIBRARY_DIRS})
ENDIF()
IF(VTK_INCLUDE_DIRS)
SET(VTK_INCLUDE_DIR ${VTK_INCLUDE_DIRS})
ENDIF()
IF(TPL_VTK_INCLUDE_DIRS)
SET(VTK_INCLUDE_DIR ${TPL_VTK_INCLUDE_DIRS})
ENDIF()
IF(TPL_VTK_LIBRARIES)
SET(VTK_LIBRARIES ${TPL_VTK_LIBRARIES})
ENDIF()
IF(NOT VTK_REQUIRED_VERSION)
SET(VTK_REQUIRED_VERSION 8.1.0)
ENDIF()
FIND_PACKAGE(VTK "${VTK_REQUIRED_VERSION}")
if(NOT VTK_FOUND)
message(" ____")
message(" __,-~~/~ `---.")
message(" _/_,---( , )")
message(" __ / < / ) \\___")
message("- ------===;;;'====------------------===;;;===----- - -")
message(" \\/ ~\"~\"~\"~\"~\"~\\~\"~)~\"/")
message(" (_ ( \\ ( > \\)")
message(" \\_( _ < >_>'")
message(" ~ `-i' ::>|--\"")
message(" I;|.|.|")
message(" <|i::|i|`.")
message(" (` ^'`-' )")
MESSAGE(FATAL_ERROR "Couldn't find VTK. This causes explosions.")
endif()
IF(NOT(VTK_INCLUDE_DIRS))
SET(VTK_INCLUDE_DIRS ${VTK_INCLUDE_DIRS})
ENDIF()
IF(NOT(VTK_LIBRARY_DIRS))
SET(VTK_LIBRARY_DIRS ${VTK_LIBRARY_DIRS})
ENDIF()
IF(NOT TPL_VTK_INCLUDE_DIRS )
SET(TPL_VTK_INCLUDE_DIRS ${VTK_INCLUDE_DIRS})
ENDIF()
IF(NOT TPL_VTK_LIBRARY_DIRS )
SET(TPL_VTK_LIBRARY_DIRS ${VTK_LIBRARY_DIRS})
ENDIF()
IF(NOT TPL_VTK_LIBRARIES)
SET(TPL_VTK_LIBRARIES ${VTK_LIBRARIES})
ENDIF()
SET(VTK_USE_FILE ${VTK_USE_FILE} CACHE STRING "VTK use file path.")
SET(TPL_VTK_LIBRARIES ${TPL_VTK_LIBRARIES} CACHE STRING "VTK TPL libraries")
SET(TPL_VTK_LIBRARY_DIRS ${TPL_VTK_LIBRARY_DIRS} CACHE STRING "VTK TPL library directories")
SET(TPL_VTK_INCLUDE_DIRS ${TPL_VTK_INCLUDE_DIRS} CACHE STRING "VTK TPL include dirs")
......@@ -2,16 +2,27 @@
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR})
IF(USE_QT4)
QT4_WRAP_CPP(QMOC_FILES
QT4_WRAP_CPP(QMOC_FILES1
radixtabwidget.hh
)
QT4_WRAP_CPP(QMOC_FILES2
radixtablewidget.hh
)
ELSE()
QT5_WRAP_CPP(QMOC_FILES
QT5_WRAP_CPP(QMOC_FILES1
radixtabwidget.hh
)
QT5_WRAP_CPP(QMOC_FILES2
radixtablewidget.hh
)
ENDIF()
TRIBITS_ADD_EXECUTABLE(radixtabwidget
NOEXEPREFIX
SOURCES radixtabwidget.cc ${QMOC_FILES}
SOURCES radixtabwidget.cc ${QMOC_FILES1}
)
TRIBITS_ADD_EXECUTABLE(radixtablewidget
NOEXEPREFIX
SOURCES radixtablewidget.cc ${QMOC_FILES2}
)
/*
* @file: mainwindow.cpp
* @author: Jordan P. Lefebvre, lefebvrejp@ornl.gov
*
* Created on June 24, 2018, 10:52 AM
*/
#include "radixtablewidget.hh"
#include <QApplication>
#include <QGridLayout>
#include <QHeaderView>
#include <QLabel>
#include <limits>
TableModel::TableModel(QObject *parent)
: QAbstractTableModel(parent)
{
}
TableModel::TableModel(QList<Contact> contacts, QObject *parent)
: QAbstractTableModel(parent)
, contacts(contacts)
{
}
int TableModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return contacts.size();
}
int TableModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return 3;
}
QVariant TableModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid()) return QVariant();
if (index.row() >= contacts.size() || index.row() < 0) return QVariant();
if (role == Qt::DisplayRole || role == Qt::EditRole)
{
const auto &contact = contacts.at(index.row());
if (index.column() == 0)
return contact.name;
else if (index.column() == 1)
return contact.address;
else
return contact.phone;
}
return QVariant();
}
QVariant TableModel::headerData(int section, Qt::Orientation orientation,
int role) const
{
if (role != Qt::DisplayRole) return QVariant();
if (orientation == Qt::Horizontal)
{
switch (section)
{
case 0:
return tr("Name");
case 1:
return tr("Address");
case 2:
return tr("Phone");
default:
return QVariant();
}
}
else if (orientation == Qt::Vertical)
{
return section;
}
return QVariant();
}
bool TableModel::insertRows(int position, int rows, const QModelIndex &index)
{
Q_UNUSED(index);
beginInsertRows(QModelIndex(), position, position + rows - 1);
for (int row = 0; row < rows; ++row)
contacts.insert(position, {QString(), QString(), QString()});
endInsertRows();
return true;
}
bool TableModel::removeRows(int position, int rows, const QModelIndex &index)
{
Q_UNUSED(index);
beginRemoveRows(QModelIndex(), position, position + rows - 1);
for (int row = 0; row < rows; ++row) contacts.removeAt(position);
endRemoveRows();
return true;
}
bool TableModel::setData(const QModelIndex &index, const QVariant &value,
int role)
{
if (index.isValid() && role == Qt::EditRole)
{
int row = index.row();
auto contact = contacts.value(row);
if (index.column() == 0)
contact.name = value.toString();
else if (index.column() == 1)
contact.address = value.toString();
else if (index.column() == 2)
contact.phone = value.toString();
else
return false;
contacts.replace(row, contact);
emit(dataChanged(index, index));
return true;
}
return false;
}
Qt::ItemFlags TableModel::flags(const QModelIndex &index) const
{
if (!index.