File indexing completed on 2024-04-28 16:32:02
0001 /*************************************************************************** 0002 Copyright (C) 2005-2009 Robby Stephenson <robby@periapsis.org> 0003 ***************************************************************************/ 0004 0005 /*************************************************************************** 0006 * * 0007 * This program is free software; you can redistribute it and/or * 0008 * modify it under the terms of the GNU General Public License as * 0009 * published by the Free Software Foundation; either version 2 of * 0010 * the License or (at your option) version 3 or any later version * 0011 * accepted by the membership of KDE e.V. (or its successor approved * 0012 * by the membership of KDE e.V.), which shall act as a proxy * 0013 * defined in Section 14 of version 3 of the license. * 0014 * * 0015 * This program is distributed in the hope that it will be useful, * 0016 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 0017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 0018 * GNU General Public License for more details. * 0019 * * 0020 * You should have received a copy of the GNU General Public License * 0021 * along with this program. If not, see <http://www.gnu.org/licenses/>. * 0022 * * 0023 ***************************************************************************/ 0024 0025 #include "loanview.h" 0026 #include "controller.h" 0027 #include "borrower.h" 0028 #include "entry.h" 0029 #include "collection.h" 0030 #include "tellico_kernel.h" 0031 #include "tellico_debug.h" 0032 #include "models/borrowermodel.h" 0033 #include "models/entrysortmodel.h" 0034 #include "models/models.h" 0035 #include "gui/countdelegate.h" 0036 0037 #include <KLocalizedString> 0038 0039 #include <QMenu> 0040 #include <QIcon> 0041 #include <QHeaderView> 0042 #include <QContextMenuEvent> 0043 0044 using Tellico::LoanView; 0045 0046 LoanView::LoanView(QWidget* parent_) : GUI::TreeView(parent_), m_notSortedYet(true) { 0047 header()->setSectionResizeMode(QHeaderView::Stretch); 0048 setHeaderHidden(false); 0049 setSelectionMode(QAbstractItemView::ExtendedSelection); 0050 0051 connect(this, &QAbstractItemView::doubleClicked, 0052 this, &LoanView::slotDoubleClicked); 0053 0054 connect(header(), &QHeaderView::sortIndicatorChanged, 0055 this, &LoanView::slotSortingChanged); 0056 0057 BorrowerModel* borrowerModel = new BorrowerModel(this); 0058 EntrySortModel* sortModel = new EntrySortModel(this); 0059 sortModel->setSourceModel(borrowerModel); 0060 setModel(sortModel); 0061 setItemDelegate(new GUI::CountDelegate(this)); 0062 updateHeader(); 0063 } 0064 0065 /* 0066 bool LoanView::isSelectable(GUI::ListViewItem* item_) const { 0067 if(!GUI::ListView::isSelectable(item_)) { 0068 return false; 0069 } 0070 0071 // because the popup menu has modify, only 0072 // allow one loan item to get selected 0073 if(item_->isLoanItem()) { 0074 return selectedItems().isEmpty(); 0075 } 0076 0077 return true; 0078 } 0079 */ 0080 Tellico::BorrowerModel* LoanView::sourceModel() const { 0081 return static_cast<BorrowerModel*>(sortModel()->sourceModel()); 0082 } 0083 0084 void LoanView::addCollection(Tellico::Data::CollPtr coll_) { 0085 m_coll = coll_; 0086 sourceModel()->clear(); 0087 sourceModel()->addBorrowers(m_coll->borrowers()); 0088 } 0089 0090 void LoanView::slotReset() { 0091 sourceModel()->clear(); 0092 } 0093 0094 void LoanView::addBorrower(Tellico::Data::BorrowerPtr borrower_) { 0095 Q_ASSERT(borrower_); 0096 sourceModel()->addBorrower(borrower_); 0097 } 0098 0099 void LoanView::modifyBorrower(Tellico::Data::BorrowerPtr borrower_) { 0100 Q_ASSERT(borrower_); 0101 sourceModel()->modifyBorrower(borrower_); 0102 } 0103 0104 void LoanView::removeBorrower(Tellico::Data::BorrowerPtr borrower_) { 0105 Q_ASSERT(borrower_); 0106 sourceModel()->removeBorrower(borrower_); 0107 } 0108 0109 void LoanView::contextMenuEvent(QContextMenuEvent* event_) { 0110 QModelIndex index = indexAt(event_->pos()); 0111 if(!index.isValid()) { 0112 return; 0113 } 0114 0115 // no parent means it's a top-level item 0116 if(index.parent().isValid()) { 0117 QMenu menu(this); 0118 menu.addAction(QIcon::fromTheme(QStringLiteral("arrow-down-double")), 0119 i18n("Check-in"), this, &LoanView::slotCheckIn); 0120 menu.addAction(QIcon::fromTheme(QStringLiteral("arrow-down-double")), 0121 i18n("Modify Loan..."), this, &LoanView::slotModifyLoan); 0122 menu.exec(event_->globalPos()); 0123 } 0124 } 0125 0126 void LoanView::slotDoubleClicked(const QModelIndex& index_) { 0127 QModelIndex realIndex = sortModel()->mapToSource(index_); 0128 Data::LoanPtr loan = sourceModel()->loan(realIndex); 0129 if(loan) { 0130 Kernel::self()->modifyLoan(loan); 0131 } 0132 } 0133 0134 // this gets called when header() is clicked, so cycle through 0135 void LoanView::slotSortingChanged(int col_, Qt::SortOrder order_) { 0136 Q_UNUSED(col_); 0137 if(order_ == Qt::AscendingOrder && !m_notSortedYet) { // cycle through after ascending 0138 if(sortModel()->sortRole() == RowCountRole) { 0139 sortModel()->setSortRole(Qt::DisplayRole); 0140 } else { 0141 sortModel()->setSortRole(RowCountRole); 0142 } 0143 } 0144 0145 updateHeader(); 0146 m_notSortedYet = false; 0147 } 0148 0149 /* 0150 BorrowerItem* item = static_cast<BorrowerItem*>(item_); 0151 Data::LoanList loans = item->borrower()->loans(); 0152 foreach(Data::LoanPtr loan, loans) { 0153 new LoanItem(item, loan); 0154 } 0155 */ 0156 0157 void LoanView::slotCheckIn() { 0158 QModelIndexList indexes = selectionModel()->selectedIndexes(); 0159 if(indexes.isEmpty()) { 0160 return; 0161 } 0162 0163 Data::EntryList entries; 0164 foreach(const QModelIndex& index, indexes) { 0165 // the indexes pointing to a borrower have no parent, skip them 0166 if(!index.parent().isValid()) { 0167 continue; 0168 } 0169 if(model()->hasChildren(index)) { //ignore items with children 0170 continue; 0171 } 0172 QModelIndex sourceIndex = sortModel()->mapToSource(index); 0173 Data::EntryPtr entry = sourceModel()->entry(sourceIndex); 0174 if(entry) { 0175 entries += entry; 0176 } 0177 } 0178 0179 Controller::self()->slotCheckIn(entries); 0180 Controller::self()->slotClearSelection(); // so the checkout menu item gets disabled 0181 } 0182 0183 void LoanView::slotModifyLoan() { 0184 QModelIndexList indexes = selectionModel()->selectedIndexes(); 0185 if(indexes.isEmpty()) { 0186 return; 0187 } 0188 0189 foreach(const QModelIndex& index, indexes) { 0190 QModelIndex sourceIndex = sortModel()->mapToSource(index); 0191 Data::LoanPtr loan = sourceModel()->loan(sourceIndex); 0192 if(loan) { 0193 Kernel::self()->modifyLoan(loan); 0194 } 0195 } 0196 } 0197 0198 void LoanView::updateHeader() { 0199 if(sortModel()->sortRole() == Qt::DisplayRole) { 0200 model()->setHeaderData(0, Qt::Horizontal, i18n("Borrower")); 0201 } else { 0202 model()->setHeaderData(0, Qt::Horizontal, i18n("Borrower (Sort by Count)")); 0203 } 0204 }