File indexing completed on 2024-05-19 04:56:04

0001 /**
0002  * \file framelist.h
0003  * List of frames.
0004  *
0005  * \b Project: Kid3
0006  * \author Urs Fleisch
0007  * \date 9 Jan 2003
0008  *
0009  * Copyright (C) 2003-2024  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 #pragma once
0028 
0029 #include <QObject>
0030 #include "frame.h"
0031 
0032 class QItemSelectionModel;
0033 class FrameTableModel;
0034 class TaggedFile;
0035 class IFrameEditor;
0036 
0037 /**
0038  * List of frames.
0039  */
0040 class KID3_CORE_EXPORT FrameList : public QObject {
0041   Q_OBJECT
0042 public:
0043   /**
0044    * Constructor.
0045    *
0046    * @param tagNr tag number
0047    * @param ftm frame table model
0048    * @param selModel item selection model
0049    */
0050   FrameList(Frame::TagNumber tagNr,
0051             FrameTableModel* ftm, QItemSelectionModel* selModel);
0052 
0053   /**
0054    * Destructor.
0055    */
0056   ~FrameList() override = default;
0057 
0058   /**
0059    * Get editor for frames.
0060    * @return frame editor.
0061    */
0062   IFrameEditor* frameEditor() const { return m_frameEditor; }
0063 
0064   /**
0065    * Set editor for frames.
0066    *
0067    * @param frameEditor frame editor
0068    */
0069   void setFrameEditor(IFrameEditor* frameEditor);
0070 
0071   /**
0072    * Set tagged file.
0073    *
0074    * @param taggedFile file
0075    */
0076   void setTaggedFile(TaggedFile* taggedFile) { m_taggedFile = taggedFile; }
0077 
0078   /**
0079    * Get tagged file.
0080    * @return tagged file.
0081    */
0082   TaggedFile* getTaggedFile() const { return m_taggedFile; }
0083 
0084   /**
0085    * Delete selected frame.
0086    *
0087    * @return false if frame not found.
0088    */
0089   bool deleteFrame();
0090 
0091   /**
0092    * Let the user select and edit a frame type and then edit the frame.
0093    * Add the frame if the edits are accepted.
0094    * frameEdited() is emitted with the added frame.
0095    */
0096   void selectAddAndEditFrame();
0097 
0098   /**
0099    * Add and edit a new frame.
0100    * frameEdited() is emitted with the added frame.
0101    */
0102   void addAndEditFrame();
0103 
0104   /**
0105    * Edit the current frame.
0106    * The frame and its file have to be set using setFrame() and setTaggedFile().
0107    */
0108   void editFrame();
0109 
0110   /**
0111    * Paste the selected frame from the copy buffer.
0112    *
0113    * @return true if frame pasted.
0114    */
0115   bool pasteFrame();
0116 
0117   /**
0118    * Get the frame in the copy buffer.
0119    * @return frame from copy buffer.
0120    */
0121   const Frame& getFrame() const { return m_frame; }
0122 
0123   /**
0124    * Set the frame in the copy buffer.
0125    * @param frame frame to set
0126    */
0127   void setFrame(const Frame& frame) { m_frame = frame; }
0128 
0129   /**
0130    * Add a suitable field list for the frame in the copy buffer if missing.
0131    */
0132   void addFrameFieldList();
0133 
0134   /**
0135    * Check if the frame in the copy buffer is a picture frame.
0136    * @return true if picture frame.
0137    */
0138   bool isPictureFrame() const { return m_frame.getType() == Frame::FT_Picture; }
0139 
0140   /**
0141    * Get the name of the selected frame.
0142    *
0143    * @return name, QString::null if nothing selected.
0144    */
0145   QString getSelectedName() const;
0146 
0147   /**
0148    * Select a frame with a given name.
0149    *
0150    * @param name name of frame
0151    *
0152    * @return true if a frame with that name could be selected.
0153    */
0154   bool selectByName(const QString& name);
0155 
0156   /**
0157    * Select a frame by row number in the frame table.
0158    *
0159    * @param row row of frame
0160    *
0161    * @return true if a frame could be selected.
0162    */
0163   Q_INVOKABLE bool selectByRow(int row);
0164 
0165   /**
0166    * Get ID of selected frame list item.
0167    *
0168    * @return ID of selected item,
0169    *         -1 if not item is selected.
0170    */
0171   int getSelectedId() const;
0172 
0173   /**
0174    * Select the frame by ID.
0175    *
0176    * @param id ID of frame to select
0177    */
0178   void setSelectedId(int id);
0179 
0180   /**
0181    * Get number of tag containing the frames of this frame list.
0182    * @return tag number.
0183    */
0184   Frame::TagNumber tagNumber() const { return m_tagNr; }
0185 
0186   /**
0187    * Save the current cursor position.
0188    */
0189   void saveCursor();
0190 
0191   /**
0192    * Restore the cursor position saved with saveCursor().
0193    */
0194   void restoreCursor();
0195 
0196 signals:
0197   /**
0198    * Emitted when the dialog to add and edit a frame is closed and an
0199    * existing frame was edited.
0200    * @param frame edited frame if dialog was accepted, else 0
0201    */
0202   void frameEdited(const Frame* frame);
0203 
0204   /**
0205    * Emitted when the dialog to add and edit a frame is closed and a new
0206    * frame was added.
0207    * @param frame edited frame if dialog was accepted, else 0
0208    */
0209   void frameAdded(const Frame* frame);
0210 
0211 private slots:
0212   void onFrameSelected(Frame::TagNumber tagNr, const Frame* frame);
0213   void onFrameEdited(Frame::TagNumber tagNr, const Frame* frame);
0214 
0215 private:
0216   FrameList(const FrameList&);
0217   FrameList& operator=(const FrameList&);
0218 
0219   /**
0220    * Get frame of selected frame list item.
0221    *
0222    * @param frame the selected frame is returned here
0223    *
0224    * @return false if not item is selected.
0225    */
0226   bool getSelectedFrame(Frame& frame) const;
0227 
0228   /**
0229    * Set the frame table model from the tagged file.
0230    */
0231   void setModelFromTaggedFile();
0232 
0233   /** Set of old changed frames stored while in the edit dialog */
0234   QList<Frame::ExtendedType> m_oldChangedFrames;
0235   /** File containing tags */
0236   TaggedFile* m_taggedFile;
0237   /** Editor for frames */
0238   IFrameEditor* m_frameEditor;
0239   /** Frame used to add, edit and paste */
0240   Frame m_frame;
0241 
0242   FrameTableModel* m_frameTableModel;
0243   QItemSelectionModel* m_selectionModel;
0244 
0245   int m_cursorRow;
0246   int m_cursorColumn;
0247   const Frame::TagNumber m_tagNr;
0248 
0249   /** true while a frame is added */
0250   bool m_addingFrame;
0251 };