File indexing completed on 2025-02-23 04:34:25

0001 /**
0002  * \file timestampdelegate.cpp
0003  * Delegate for time stamps in synchronized lyrics and event timing codes.
0004  *
0005  * \b Project: Kid3
0006  * \author Urs Fleisch
0007  * \date 14 Mar 2014
0008  *
0009  * Copyright (C) 2014-2018  Urs Fleisch
0010  *
0011  * This file is part of Kid3.
0012  *
0013  * Kid3 is free software; you can redistribute it and/or modify
0014  * it under the terms of the GNU General Public License as published by
0015  * the Free Software Foundation; either version 2 of the License, or
0016  * (at your option) any later version.
0017  *
0018  * Kid3 is distributed in the hope that it will be useful,
0019  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0020  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0021  * GNU General Public License for more details.
0022  *
0023  * You should have received a copy of the GNU General Public License
0024  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0025  */
0026 
0027 #include "timestampdelegate.h"
0028 #include <QTime>
0029 #include <QTimeEdit>
0030 #include "timeeventmodel.h"
0031 
0032 /**
0033  * Constructor.
0034  * @param parent parent QTableView
0035  */
0036 TimeStampDelegate::TimeStampDelegate(QObject* parent) : QItemDelegate(parent)
0037 {
0038   setObjectName(QLatin1String("TimeStampDelegate"));
0039 }
0040 
0041 /**
0042  * Create an editor to edit the cell contents.
0043  * @param parent parent widget
0044  * @param index  index of item
0045  * @return combo box editor widget.
0046  */
0047 QWidget* TimeStampDelegate::createEditor(
0048   QWidget* parent, const QStyleOptionViewItem&, const QModelIndex& index) const
0049 {
0050   QTime time = index.data().toTime();
0051   auto timeEdit = new QTimeEdit(parent);
0052   timeEdit->setDisplayFormat(time.hour() == 0 ? QLatin1String("mm:ss.zzz")
0053                                               : QLatin1String("hh:mm:ss.zzz"));
0054   connect(timeEdit, &QAbstractSpinBox::editingFinished,
0055           this, &TimeStampDelegate::commitAndCloseEditor);
0056   return timeEdit;
0057 }
0058 
0059 /**
0060  * Commit and close the editor.
0061  */
0062 void TimeStampDelegate::commitAndCloseEditor()
0063 {
0064   if (auto editor = qobject_cast<QTimeEdit*>(sender())) {
0065     emit commitData(editor);
0066     emit closeEditor(editor);
0067   }
0068 }
0069 
0070 /**
0071  * Render delegate.
0072  * @param painter painter to be used
0073  * @param option style
0074  * @param index index of item
0075  */
0076 void TimeStampDelegate::paint(QPainter* painter,
0077                               const QStyleOptionViewItem& option,
0078                               const QModelIndex& index) const
0079 {
0080   QTime time = index.data().toTime();
0081   QString text = TimeEventModel::timeStampToString(time);
0082   QStyleOptionViewItem opt = option;
0083   opt.displayAlignment = Qt::AlignRight | Qt::AlignVCenter;
0084   drawDisplay(painter, opt, opt.rect, text);
0085   drawFocus(painter, opt, opt.rect);
0086 }