Warning, file /sdk/cervisia/loglist.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002  *  Copyright (C) 1999-2002 Bernd Gehrmann
0003  *                          bernd@mail.berlios.de
0004  *
0005  * This program is free software; you can redistribute it and/or modify
0006  * it under the terms of the GNU General Public License as published by
0007  * the Free Software Foundation; either version 2 of the License, or
0008  * (at your option) any later version.
0009  *
0010  * This program is distributed in the hope that it will be useful,
0011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013  * GNU General Public License for more details.
0014  *
0015  * You should have received a copy of the GNU General Public License
0016  * along with this program; if not, write to the Free Software
0017  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
0018  */
0019 
0020 #include "loglist.h"
0021 
0022 #include <qapplication.h>
0023 #include <qnamespace.h>
0024 
0025 #include <QHeaderView>
0026 
0027 #include <KConfig>
0028 #include <KConfigGroup>
0029 #include <klocalizedstring.h>
0030 
0031 #include "loginfo.h"
0032 #include "misc.h"
0033 #include "tooltip.h"
0034 
0035 class LogListViewItem : public QTreeWidgetItem
0036 {
0037 public:
0038     enum { Revision, Author, Date, Branch, Comment, Tags };
0039 
0040     LogListViewItem(QTreeWidget *list, const Cervisia::LogInfo &logInfo);
0041 
0042     bool operator<(const QTreeWidgetItem &other) const override;
0043 
0044 private:
0045     static QString truncateLine(const QString &s);
0046 
0047     Cervisia::LogInfo m_logInfo;
0048     friend class LogListView;
0049 };
0050 
0051 LogListViewItem::LogListViewItem(QTreeWidget *list, const Cervisia::LogInfo &logInfo)
0052     : QTreeWidgetItem(list)
0053     , m_logInfo(logInfo)
0054 {
0055     setText(Revision, logInfo.m_revision);
0056     setText(Author, logInfo.m_author);
0057     setText(Date, logInfo.dateTimeToString());
0058     setText(Comment, truncateLine(logInfo.m_comment));
0059 
0060     for (Cervisia::LogInfo::TTagInfoSeq::const_iterator it = logInfo.m_tags.begin(); it != logInfo.m_tags.end(); ++it) {
0061         const Cervisia::TagInfo &tagInfo(*it);
0062 
0063         if (tagInfo.m_type == Cervisia::TagInfo::OnBranch) {
0064             setText(Branch, tagInfo.m_name);
0065         }
0066     }
0067 
0068     setText(Tags, logInfo.tagsToString(Cervisia::TagInfo::Tag, Cervisia::LogInfo::NoTagType, QLatin1String(", ")));
0069 }
0070 
0071 QString LogListViewItem::truncateLine(const QString &s)
0072 {
0073     int pos;
0074 
0075     QString res = s.simplified();
0076     if ((pos = res.indexOf('\n')) != -1)
0077         res = res.left(pos) + "...";
0078 
0079     return res;
0080 }
0081 
0082 bool LogListViewItem::operator<(const QTreeWidgetItem &other) const
0083 {
0084     const auto &item = static_cast<const LogListViewItem &>(other);
0085 
0086     switch (treeWidget()->sortColumn()) {
0087     case Revision:
0088         return ::compareRevisions(m_logInfo.m_revision, item.m_logInfo.m_revision) == -1;
0089     case Date:
0090         return ::compare(m_logInfo.m_dateTime, item.m_logInfo.m_dateTime) == -1;
0091     }
0092 
0093     return QTreeWidgetItem::operator<(other);
0094 }
0095 
0096 LogListView::LogListView(KConfig &cfg, QWidget *parent)
0097     : QTreeWidget(parent)
0098     , partConfig(cfg)
0099 {
0100     setAllColumnsShowFocus(true);
0101     header()->setSortIndicatorShown(true);
0102     setSelectionMode(QAbstractItemView::NoSelection);
0103     setRootIsDecorated(false);
0104     setSortingEnabled(true);
0105     sortByColumn(LogListViewItem::Revision, Qt::DescendingOrder);
0106     setHeaderLabels(QStringList() << i18n("Revision") << i18n("Author") << i18n("Date") << i18n("Branch") << i18n("Comment") << i18n("Tags"));
0107 
0108     auto toolTip = new Cervisia::ToolTip(viewport());
0109 
0110     connect(toolTip, SIGNAL(queryToolTip(QPoint, QRect &, QString &)), this, SLOT(slotQueryToolTip(QPoint, QRect &, QString &)));
0111 
0112     QByteArray state = cfg.group("LogList view").readEntry<QByteArray>("Columns", QByteArray());
0113     header()->restoreState(state);
0114 }
0115 
0116 LogListView::~LogListView()
0117 {
0118     partConfig.group("LogList view").writeEntry("Columns", header()->saveState());
0119 }
0120 
0121 void LogListView::addRevision(const Cervisia::LogInfo &logInfo)
0122 {
0123     (void)new LogListViewItem(this, logInfo);
0124 }
0125 
0126 void LogListView::setSelectedPair(const QString &selectionA, const QString &selectionB)
0127 {
0128     for (int j = 0; j < topLevelItemCount(); j++) {
0129         auto i = static_cast<LogListViewItem *>(topLevelItem(j));
0130         i->setSelected(selectionA == i->text(LogListViewItem::Revision) || selectionB == i->text(LogListViewItem::Revision));
0131     }
0132 }
0133 
0134 void LogListView::mousePressEvent(QMouseEvent *e)
0135 {
0136     // Retrieve selected item
0137     const LogListViewItem *selItem = static_cast<LogListViewItem *>(itemAt(e->pos()));
0138     if (!selItem)
0139         return;
0140 
0141     // Retrieve revision
0142     const QString revision = selItem->text(LogListViewItem::Revision);
0143 
0144     if (e->button() == Qt::LeftButton) {
0145         // If the control key was pressed, then we change revision B not A
0146         if (e->modifiers() & Qt::ControlModifier)
0147             emit revisionClicked(revision, true);
0148         else
0149             emit revisionClicked(revision, false);
0150     } else if (e->button() == Qt::MiddleButton)
0151         emit revisionClicked(revision, true);
0152 }
0153 
0154 void LogListView::keyPressEvent(QKeyEvent *e)
0155 {
0156     switch (e->key()) {
0157     case Qt::Key_A:
0158         if (currentItem())
0159             emit revisionClicked(currentItem()->text(LogListViewItem::Revision), false);
0160         break;
0161         break;
0162     case Qt::Key_B:
0163         if (currentItem())
0164             emit revisionClicked(currentItem()->text(LogListViewItem::Revision), true);
0165         break;
0166     case Qt::Key_Backspace:
0167     case Qt::Key_Delete:
0168     case Qt::Key_Down:
0169     case Qt::Key_Up:
0170     case Qt::Key_Home:
0171     case Qt::Key_End:
0172     case Qt::Key_PageDown:
0173     case Qt::Key_PageUp:
0174         if (e->modifiers() == Qt::NoModifier)
0175             QTreeWidget::keyPressEvent(e);
0176         else
0177             QApplication::postEvent(this, new QKeyEvent(QEvent::KeyPress, e->key(), Qt::NoModifier, e->text()));
0178         break;
0179     default:
0180         // Ignore Key_Enter, Key_Return
0181         e->ignore();
0182     }
0183 }
0184 
0185 void LogListView::slotQueryToolTip(const QPoint &viewportPos, QRect &viewportRect, QString &text)
0186 {
0187     if (const LogListViewItem *item = static_cast<LogListViewItem *>(itemAt(viewportPos))) {
0188         viewportRect = visualRect(indexAt(viewportPos));
0189         text = item->m_logInfo.createToolTipText();
0190     }
0191 }
0192 
0193 // Local Variables:
0194 // c-basic-offset: 4
0195 // End: