File indexing completed on 2024-05-12 16:41:08
0001 /*********************************************************************************** 0002 Copyright (C) 2011-2012 by Holger Danielsson (holger.danielsson@versanet.de) 0003 ***********************************************************************************/ 0004 0005 /*************************************************************************** 0006 * * 0007 * This program is free software; you can redistribute it and/or modify * 0008 * it under the terms of the GNU General Public License as published by * 0009 * the Free Software Foundation; either version 2 of the License, or * 0010 * (at your option) any later version. * 0011 * * 0012 ***************************************************************************/ 0013 0014 0015 #include <QFile> 0016 #include <QFileInfo> 0017 0018 #include "dialogs/usermenu/usermenuitem.h" 0019 0020 0021 namespace KileMenu { 0022 0023 UserMenuItem::UserMenuItem(UserMenuData::MenuType type, const QString &menutitle) 0024 : QTreeWidgetItem() 0025 { 0026 initItem(type,menutitle); 0027 } 0028 0029 UserMenuItem::UserMenuItem( QTreeWidget *parent, QTreeWidgetItem *preceding, UserMenuData::MenuType type, const QString &menutitle) 0030 : QTreeWidgetItem(parent,preceding) 0031 { 0032 initItem(type,menutitle); 0033 } 0034 0035 UserMenuItem::UserMenuItem( QTreeWidgetItem *parent, QTreeWidgetItem *preceding, UserMenuData::MenuType type, const QString &menutitle) 0036 : QTreeWidgetItem(parent,preceding) 0037 { 0038 initItem(type,menutitle); 0039 } 0040 0041 void UserMenuItem::initItem(UserMenuData::MenuType type, const QString &menutitle) 0042 { 0043 clear(); 0044 setText(0,menutitle); 0045 0046 m_data.menutitle = menutitle; 0047 m_data.menutype = type; 0048 0049 setData(0, Qt::UserRole+1, UserMenuData::xmlMenuTypeName(type)); 0050 setData(0, Qt::UserRole+2, MODEL_ERROR_NONE); 0051 } 0052 0053 // check for possible errors and save for use with model data 0054 void UserMenuItem::setModelData(bool executable) 0055 { 0056 int modelerror = MODEL_ERROR_NONE; 0057 0058 if ( m_data.menutitle.isEmpty() && m_data.menutype!=UserMenuData::Separator ) { 0059 modelerror |= UserMenuItem::MODEL_ERROR_EMPTY; 0060 } 0061 0062 if ( m_data.menutype==UserMenuData::Submenu && childCount()==0 ) { 0063 modelerror |= UserMenuItem::MODEL_ERROR_SUBMENU; 0064 } 0065 else if ( m_data.menutype==UserMenuData::Text && m_data.text.isEmpty() ) { 0066 modelerror |= UserMenuItem::MODEL_ERROR_TEXT; 0067 } 0068 else if ( m_data.menutype == UserMenuData::FileContent ) { 0069 if ( m_data.filename.isEmpty() ) { 0070 modelerror |= UserMenuItem::MODEL_ERROR_FILE_EMPTY; 0071 } 0072 else if ( !QFile::exists(m_data.filename) ) { 0073 modelerror |= UserMenuItem::MODEL_ERROR_FILE_EXIST; 0074 } 0075 } 0076 else if ( m_data.menutype == UserMenuData::Program && !executable ) { 0077 modelerror |= UserMenuItem::MODEL_ERROR_FILE_EXECUTABLE; 0078 } 0079 0080 setData(0,Qt::UserRole+2,modelerror); 0081 } 0082 0083 // two possible errors in the menutree are made visible for the user 0084 // - if no menutitle is given, it is changed to '???' 0085 // - if a (useless) submenu with no children is given, the menutitle 'title' is changed to 'title >' 0086 QString UserMenuItem::updateMenutitle() 0087 { 0088 QString menutitle = m_data.menutitle; 0089 if ( menutitle.isEmpty() ) { 0090 menutitle = EMPTY_MENUENTRY; 0091 } 0092 else if ( m_data.menutype==UserMenuData::Submenu && childCount()==0 ) { 0093 menutitle += EMPTY_SUBMENU; 0094 } 0095 return menutitle; 0096 } 0097 0098 0099 void UserMenuItem::clear() 0100 { 0101 m_data.clear(); 0102 } 0103 0104 0105 }