File indexing completed on 2024-05-12 16:41:07
0001 /******************************************************************************************** 0002 begin : Sunday Jun 27 2008 0003 copyright : (C) 2008 by Mathias Soeken (msoeken@informatik.uni-bremen.de) 0004 copyright : (C) 2005-2006 by Holger Danielsson (holger.danielsson@t-online.de) 0005 ********************************************************************************************/ 0006 0007 /*************************************************************************** 0008 * * 0009 * This program is free software; you can redistribute it and/or modify * 0010 * it under the terms of the GNU General Public License as published by * 0011 * the Free Software Foundation; either version 2 of the License, or * 0012 * (at your option) any later version. * 0013 * * 0014 ***************************************************************************/ 0015 0016 #include "tabularheaderitem.h" 0017 0018 #include <QAction> 0019 #include <QIcon> 0020 #include <QMenu> 0021 0022 #include <KLocalizedString> 0023 0024 namespace KileDialog { 0025 0026 TabularHeaderItem::TabularHeaderItem(QWidget *parent) 0027 : QObject(parent), 0028 QTableWidgetItem(QIcon::fromTheme("format-justify-left"), "l"), 0029 m_Alignment(Qt::AlignLeft), 0030 m_InsertBefore(false), 0031 m_InsertAfter(false), 0032 m_SuppressSpace(false), 0033 m_DontSuppressSpace(false), 0034 m_hasXAlignment(false) 0035 { 0036 m_Popup = new QMenu(parent); 0037 m_Popup->addAction(QIcon::fromTheme("format-justify-left"), i18n("Align Left"), this, SLOT(slotAlignLeft())); 0038 m_Popup->addAction(QIcon::fromTheme("format-justify-center"), i18n("Align Center"), this, SLOT(slotAlignCenter())); 0039 m_Popup->addAction(QIcon::fromTheme("format-justify-right"), i18n("Align Right"), this, SLOT(slotAlignRight())); 0040 m_Popup->addAction(i18n("p{w} Alignment"), this, SLOT(slotAlignP())); 0041 m_Popup->addAction(i18n("b{w} Alignment"), this, SLOT(slotAlignB())); 0042 m_Popup->addAction(i18n("m{w} Alignment"), this, SLOT(slotAlignM())); 0043 m_acXAlignment = m_Popup->addAction(i18n("X Alignment"), this, SLOT(slotAlignX())); 0044 m_Popup->addSeparator(); 0045 m_acDeclPre = m_Popup->addAction(i18n("Insert Before Declaration"), this, SLOT(slotDeclPre())); 0046 m_acDeclPost = m_Popup->addAction(i18n("Insert After Declaration"), this, SLOT(slotDeclPost())); 0047 m_acDeclAt = m_Popup->addAction(i18n("Suppress Space"), this, SLOT(slotDeclAt())); 0048 m_acDeclBang = m_Popup->addAction(i18n("Do not Suppress Space"), this, SLOT(slotDeclBang())); 0049 0050 m_acDeclPre->setCheckable(true); 0051 m_acDeclPost->setCheckable(true); 0052 m_acDeclAt->setCheckable(true); 0053 m_acDeclBang->setCheckable(true); 0054 } 0055 0056 void TabularHeaderItem::setAlignment(int alignment) 0057 { 0058 m_Alignment = alignment; 0059 format(); 0060 } 0061 0062 int TabularHeaderItem::alignment() const 0063 { 0064 return m_Alignment; 0065 } 0066 0067 bool TabularHeaderItem::insertBefore() const 0068 { 0069 return m_InsertBefore; 0070 } 0071 0072 bool TabularHeaderItem::insertAfter() const 0073 { 0074 return m_InsertAfter; 0075 } 0076 0077 bool TabularHeaderItem::suppressSpace() const 0078 { 0079 return m_SuppressSpace; 0080 } 0081 0082 bool TabularHeaderItem::dontSuppressSpace() const 0083 { 0084 return m_DontSuppressSpace; 0085 } 0086 0087 void TabularHeaderItem::setHasXAlignment(bool hasXAlignment) 0088 { 0089 m_hasXAlignment = hasXAlignment; 0090 if(!hasXAlignment && m_Alignment == AlignX) { 0091 slotAlignLeft(); 0092 } 0093 } 0094 0095 bool TabularHeaderItem::hasXAlignment() const 0096 { 0097 return m_hasXAlignment; 0098 } 0099 0100 QMenu* TabularHeaderItem::popupMenu() const 0101 { 0102 m_acXAlignment->setVisible(m_hasXAlignment); 0103 return m_Popup; 0104 } 0105 0106 void TabularHeaderItem::format() 0107 { 0108 setIcon(iconForAlignment(m_Alignment)); 0109 0110 QString text; 0111 0112 if(m_SuppressSpace) { 0113 text += '@'; 0114 } else if(m_DontSuppressSpace) { 0115 text += '!'; 0116 } 0117 if(m_InsertBefore) { 0118 text += '>'; 0119 } 0120 0121 switch(m_Alignment) { 0122 case Qt::AlignLeft: 0123 text += 'l'; 0124 break; 0125 case Qt::AlignHCenter: 0126 text += 'c'; 0127 break; 0128 case Qt::AlignRight: 0129 text += 'r'; 0130 break; 0131 case AlignP: 0132 text += 'p'; 0133 break; 0134 case AlignB: 0135 text += 'b'; 0136 break; 0137 case AlignM: 0138 text += 'm'; 0139 break; 0140 case AlignX: 0141 text += 'X'; 0142 break; 0143 } 0144 0145 if(m_InsertAfter) { 0146 text += '<'; 0147 } 0148 0149 setText(text); 0150 } 0151 0152 inline QIcon TabularHeaderItem::iconForAlignment(int alignment) const 0153 { 0154 switch(alignment) { 0155 case Qt::AlignLeft: 0156 return QIcon::fromTheme("format-justify-left"); 0157 case Qt::AlignHCenter: 0158 return QIcon::fromTheme("format-justify-center"); 0159 case Qt::AlignRight: 0160 return QIcon::fromTheme("format-justify-right"); 0161 default: 0162 return QIcon(); 0163 } 0164 } 0165 0166 void TabularHeaderItem::slotAlignLeft() 0167 { 0168 setAlignment(Qt::AlignLeft); 0169 emit alignColumn(Qt::AlignLeft); 0170 } 0171 0172 void TabularHeaderItem::slotAlignCenter() 0173 { 0174 setAlignment(Qt::AlignHCenter); 0175 emit alignColumn(Qt::AlignHCenter); 0176 } 0177 0178 void TabularHeaderItem::slotAlignRight() 0179 { 0180 setAlignment(Qt::AlignRight); 0181 emit alignColumn(Qt::AlignRight); 0182 } 0183 0184 void TabularHeaderItem::slotAlignP() 0185 { 0186 setAlignment(AlignP); 0187 emit alignColumn(AlignP); 0188 } 0189 0190 void TabularHeaderItem::slotAlignB() 0191 { 0192 setAlignment(AlignB); 0193 emit alignColumn(AlignB); 0194 } 0195 0196 void TabularHeaderItem::slotAlignM() 0197 { 0198 setAlignment(AlignM); 0199 emit alignColumn(AlignM); 0200 } 0201 0202 void TabularHeaderItem::slotAlignX() 0203 { 0204 setAlignment(AlignX); 0205 emit alignColumn(AlignX); 0206 } 0207 0208 void TabularHeaderItem::slotDeclPre() 0209 { 0210 m_InsertBefore = m_acDeclPre->isChecked(); 0211 format(); 0212 } 0213 0214 void TabularHeaderItem::slotDeclPost() 0215 { 0216 m_InsertAfter = m_acDeclPost->isChecked(); 0217 format(); 0218 } 0219 0220 void TabularHeaderItem::slotDeclAt() 0221 { 0222 m_SuppressSpace = m_acDeclAt->isChecked(); 0223 if(m_SuppressSpace) { 0224 m_DontSuppressSpace = false; 0225 m_acDeclBang->setChecked(false); 0226 } 0227 format(); 0228 } 0229 0230 void TabularHeaderItem::slotDeclBang() 0231 { 0232 m_DontSuppressSpace = m_acDeclBang->isChecked(); 0233 if(m_DontSuppressSpace) { 0234 m_SuppressSpace = false; 0235 m_acDeclAt->setChecked(false); 0236 } 0237 format(); 0238 } 0239 0240 } 0241