File indexing completed on 2024-12-08 05:08:36

0001 /***************************************************************************
0002  *   Copyright (C) 2005 by David Saxton                                    *
0003  *   david@bluehaze.org                                                    *
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 
0011 #ifndef FILEMETAINFO_H
0012 #define FILEMETAINFO_H
0013 
0014 #include "outputmethoddlg.h"
0015 
0016 #include <QList>
0017 #include <QMap>
0018 #include <QUrl>
0019 
0020 class TextDocument;
0021 class TextView;
0022 class KConfig;
0023 class KConfigGroup;
0024 typedef QList<int> IntList;
0025 
0026 class MetaInfo
0027 {
0028 public:
0029     MetaInfo();
0030 
0031     /**
0032      * Returns true if all the data stored is default; and therefore does
0033      * not need saving.
0034      */
0035     bool hasDefaultData() const;
0036     /**
0037      * Writes to the given config the data stored in here. Does not set the
0038      * group.
0039      */
0040     void save(KConfigGroup *conf);
0041     /**
0042      * Reads in the data from the config. Does not set the group.
0043      */
0044     void load(KConfigGroup *conf);
0045 
0046     IntList bookmarks() const
0047     {
0048         return m_bookmarks;
0049     }
0050     void setBookmarks(IntList bookmarks)
0051     {
0052         m_bookmarks = bookmarks;
0053     }
0054 
0055     IntList breakpoints() const
0056     {
0057         return m_breakpoints;
0058     }
0059     void setBreakpoints(IntList breakpoints)
0060     {
0061         m_breakpoints = breakpoints;
0062     }
0063 
0064     OutputMethodInfo &outputMethodInfo()
0065     {
0066         return m_outputMethodInfo;
0067     }
0068     void setOutputMethodInfo(OutputMethodInfo info)
0069     {
0070         m_outputMethodInfo = info;
0071     }
0072 
0073     unsigned cursorLine() const
0074     {
0075         return m_cursorLine;
0076     }
0077     void setCursorLine(unsigned line)
0078     {
0079         m_cursorLine = line;
0080     }
0081 
0082     unsigned cursorColumn() const
0083     {
0084         return m_cursorColumn;
0085     }
0086     void setCursorColumn(unsigned column)
0087     {
0088         m_cursorColumn = column;
0089     }
0090 
0091 protected:
0092     /**
0093      * Convert the id (e.g. "Direct") to a method, used when reading in the
0094      * config file.
0095      */
0096     OutputMethodInfo::Method::Type toMethod(const QString &id);
0097     /**
0098      * Conver the method (e.g. OutputMethodInfo::Method::Direct) to an id
0099      * that can be saved in the config file.
0100      */
0101     QString toID(OutputMethodInfo::Method::Type method);
0102 
0103     IntList m_bookmarks;
0104     IntList m_breakpoints;
0105     OutputMethodInfo m_outputMethodInfo;
0106     unsigned m_cursorLine;
0107     unsigned m_cursorColumn;
0108 };
0109 typedef QMap<QUrl, MetaInfo> MetaInfoMap;
0110 
0111 /**
0112 Looks after per-file metainfo; e.g. bookmarks, breakpoints, compiling options, etc
0113 
0114 @author David Saxton
0115 */
0116 class FileMetaInfo : public QObject
0117 {
0118     Q_OBJECT
0119 public:
0120     ~FileMetaInfo() override;
0121 
0122     /**
0123      * Initialize the TextDocument with the appropriate stored metainfo - e.g.
0124      * setting the appopriate bookmarks, etc
0125      */
0126     void initializeFromMetaInfo(const QUrl &url, TextDocument *textDocument);
0127     /**
0128      * Initialize the TextView with the appropriate stored metainfo - e.g.
0129      * setting the appopriate cursor position, etc.
0130      */
0131     void initializeFromMetaInfo(const QUrl &url, TextView *textView);
0132     /**
0133      * Initialize the OutputMethodDlg with the options the user had selected
0134      * for the last time it was used for the given url.
0135      */
0136     void initializeFromMetaInfo(const QUrl &url, OutputMethodDlg *outputMethodDlg);
0137     /**
0138      * Get the bookmarks, etc from the given TextDocument, and save them
0139      */
0140     void grabMetaInfo(const QUrl &url, TextDocument *textDocument);
0141     /**
0142      * Get the cursor position, etc from the given TextView, and save them.
0143      */
0144     void grabMetaInfo(const QUrl &url, TextView *textView);
0145     /**
0146      * Get the output method et al from the given OutputMethodDlg, and save
0147      * them.
0148      */
0149     void grabMetaInfo(const QUrl &url, OutputMethodDlg *outputMethodDlg);
0150     /**
0151      * Save all metainfo to disk.
0152      */
0153     void saveAllMetaInfo();
0154 
0155 protected:
0156     /**
0157      * Load all metainfo from disk (combining that read in with those already
0158      * loaded)
0159      */
0160     void loadAllMetaInfo();
0161 
0162     KConfig *m_metaInfoConfig;
0163 
0164 private:
0165     FileMetaInfo();
0166     friend inline FileMetaInfo *fileMetaInfo();
0167 
0168     MetaInfoMap m_metaInfoMap;
0169 };
0170 
0171 inline FileMetaInfo *fileMetaInfo()
0172 {
0173     static FileMetaInfo *fmi = new FileMetaInfo();
0174     return fmi;
0175 }
0176 
0177 #endif