File indexing completed on 2024-04-28 05:08:24

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 "collection.h"
0029 #include "tellico_kernel.h"
0030 #include "models/borrowermodel.h"
0031 #include "models/entrysortmodel.h"
0032 #include "models/models.h"
0033 #include "gui/countdelegate.h"
0034 
0035 #include <KLocalizedString>
0036 
0037 #include <QMenu>
0038 #include <QIcon>
0039 #include <QHeaderView>
0040 #include <QContextMenuEvent>
0041 
0042 using Tellico::LoanView;
0043 
0044 LoanView::LoanView(QWidget* parent_) : GUI::TreeView(parent_), m_notSortedYet(true) {
0045   header()->setSectionResizeMode(QHeaderView::Stretch);
0046   setHeaderHidden(false);
0047   setSelectionMode(QAbstractItemView::ExtendedSelection);
0048 
0049   connect(this, &QAbstractItemView::doubleClicked,
0050           this, &LoanView::slotDoubleClicked);
0051 
0052   connect(header(), &QHeaderView::sortIndicatorChanged,
0053           this, &LoanView::slotSortingChanged);
0054 
0055   BorrowerModel* borrowerModel = new BorrowerModel(this);
0056   EntrySortModel* sortModel = new EntrySortModel(this);
0057   sortModel->setSourceModel(borrowerModel);
0058   setModel(sortModel);
0059   setItemDelegate(new GUI::CountDelegate(this));
0060   updateHeader();
0061 }
0062 
0063 /*
0064 bool LoanView::isSelectable(GUI::ListViewItem* item_) const {
0065   if(!GUI::ListView::isSelectable(item_)) {
0066     return false;
0067   }
0068 
0069   // because the popup menu has modify, only
0070   // allow one loan item to get selected
0071   if(item_->isLoanItem()) {
0072     return selectedItems().isEmpty();
0073   }
0074 
0075   return true;
0076 }
0077 */
0078 Tellico::BorrowerModel* LoanView::sourceModel() const {
0079   return static_cast<BorrowerModel*>(sortModel()->sourceModel());
0080 }
0081 
0082 void LoanView::addCollection(Tellico::Data::CollPtr coll_) {
0083   m_coll = coll_;
0084   sourceModel()->clear();
0085   sourceModel()->addBorrowers(m_coll->borrowers());
0086 }
0087 
0088 void LoanView::slotReset() {
0089   sourceModel()->clear();
0090 }
0091 
0092 void LoanView::addBorrower(Tellico::Data::BorrowerPtr borrower_) {
0093   Q_ASSERT(borrower_);
0094   sourceModel()->addBorrower(borrower_);
0095 }
0096 
0097 void LoanView::modifyBorrower(Tellico::Data::BorrowerPtr borrower_) {
0098   Q_ASSERT(borrower_);
0099   sourceModel()->modifyBorrower(borrower_);
0100 }
0101 
0102 void LoanView::removeBorrower(Tellico::Data::BorrowerPtr borrower_) {
0103   Q_ASSERT(borrower_);
0104   sourceModel()->removeBorrower(borrower_);
0105 }
0106 
0107 void LoanView::contextMenuEvent(QContextMenuEvent* event_) {
0108   QModelIndex index = indexAt(event_->pos());
0109   if(!index.isValid()) {
0110     return;
0111   }
0112 
0113   // no parent means it's a top-level item
0114   if(index.parent().isValid()) {
0115     QMenu menu(this);
0116     menu.addAction(QIcon::fromTheme(QStringLiteral("arrow-down-double")),
0117                    i18n("Check-in"), this, &LoanView::slotCheckIn);
0118     menu.addAction(QIcon::fromTheme(QStringLiteral("arrow-down-double")),
0119                    i18n("Modify Loan..."), this, &LoanView::slotModifyLoan);
0120     menu.exec(event_->globalPos());
0121   }
0122 }
0123 
0124 void LoanView::slotDoubleClicked(const QModelIndex& index_) {
0125   QModelIndex realIndex = sortModel()->mapToSource(index_);
0126   Data::LoanPtr loan = sourceModel()->loan(realIndex);
0127   if(loan) {
0128     Kernel::self()->modifyLoan(loan);
0129   }
0130 }
0131 
0132 // this gets called when header() is clicked, so cycle through
0133 void LoanView::slotSortingChanged(int col_, Qt::SortOrder order_) {
0134   Q_UNUSED(col_);
0135   if(order_ == Qt::AscendingOrder && !m_notSortedYet) { // cycle through after ascending
0136     if(sortModel()->sortRole() == RowCountRole) {
0137       sortModel()->setSortRole(Qt::DisplayRole);
0138     } else {
0139       sortModel()->setSortRole(RowCountRole);
0140     }
0141   }
0142 
0143   updateHeader();
0144   m_notSortedYet = false;
0145 }
0146 
0147 /*
0148 BorrowerItem* item = static_cast<BorrowerItem*>(item_);
0149   Data::LoanList loans = item->borrower()->loans();
0150   foreach(Data::LoanPtr loan, loans) {
0151     new LoanItem(item, loan);
0152   }
0153 */
0154 
0155 void LoanView::slotCheckIn() {
0156   QModelIndexList indexes = selectionModel()->selectedIndexes();
0157   if(indexes.isEmpty()) {
0158     return;
0159   }
0160 
0161   Data::EntryList entries;
0162   foreach(const QModelIndex& index, indexes) {
0163     // the indexes pointing to a borrower have no parent, skip them
0164     if(!index.parent().isValid()) {
0165       continue;
0166     }
0167     if(model()->hasChildren(index)) { //ignore items with children
0168       continue;
0169     }
0170     QModelIndex sourceIndex = sortModel()->mapToSource(index);
0171     Data::EntryPtr entry = sourceModel()->entry(sourceIndex);
0172     if(entry) {
0173       entries += entry;
0174     }
0175   }
0176 
0177   Controller::self()->slotCheckIn(entries);
0178   Controller::self()->slotClearSelection(); // so the checkout menu item gets disabled
0179 }
0180 
0181 void LoanView::slotModifyLoan() {
0182   QModelIndexList indexes = selectionModel()->selectedIndexes();
0183   if(indexes.isEmpty()) {
0184     return;
0185   }
0186 
0187   foreach(const QModelIndex& index, indexes) {
0188     QModelIndex sourceIndex = sortModel()->mapToSource(index);
0189     Data::LoanPtr loan = sourceModel()->loan(sourceIndex);
0190     if(loan) {
0191       Kernel::self()->modifyLoan(loan);
0192     }
0193   }
0194 }
0195 
0196 void LoanView::updateHeader() {
0197   if(sortModel()->sortRole() == Qt::DisplayRole) {
0198     model()->setHeaderData(0, Qt::Horizontal, i18n("Borrower"));
0199   } else {
0200     model()->setHeaderData(0, Qt::Horizontal, i18n("Borrower (Sort by Count)"));
0201   }
0202 }