[video] fix selection after changing video or extra art
[xbmc.git] / xbmc / pvr / guilib / GUIEPGGridContainerModel.h
blob2d99c568fe24a409f4fe1e34e7623537c87fad55
1 /*
2 * Copyright (C) 2012-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
9 #pragma once
11 #include "XBDateTime.h"
13 #include <functional>
14 #include <map>
15 #include <memory>
16 #include <unordered_map>
17 #include <utility>
18 #include <vector>
20 class CFileItem;
21 class CFileItemList;
23 namespace PVR
25 struct GridItem
27 GridItem(const std::shared_ptr<CFileItem>& _item, float _width, int _startBlock, int _endBlock)
28 : item(_item), originWidth(_width), width(_width), startBlock(_startBlock), endBlock(_endBlock)
32 bool operator==(const GridItem& other) const
34 return (startBlock == other.startBlock && endBlock == other.endBlock);
37 std::shared_ptr<CFileItem> item;
38 float originWidth = 0.0f;
39 float width = 0.0f;
40 int startBlock = 0;
41 int endBlock = 0;
44 class CPVREpgInfoTag;
46 class CGUIEPGGridContainerModel
48 public:
49 static constexpr int MINSPERBLOCK = 5; // minutes
51 CGUIEPGGridContainerModel() = default;
52 virtual ~CGUIEPGGridContainerModel() = default;
54 void Initialize(const std::unique_ptr<CFileItemList>& items,
55 const CDateTime& gridStart,
56 const CDateTime& gridEnd,
57 int iFirstChannel,
58 int iChannelsPerPage,
59 int iFirstBlock,
60 int iBlocksPerPage,
61 int iRulerUnit,
62 float fBlockSize);
63 void SetInvalid();
65 static const int INVALID_INDEX = -1;
66 void FindChannelAndBlockIndex(int channelUid,
67 unsigned int broadcastUid,
68 int eventOffset,
69 int& newChannelIndex,
70 int& newBlockIndex) const;
72 void FreeChannelMemory(int keepStart, int keepEnd);
73 bool FreeProgrammeMemory(int firstChannel, int lastChannel, int firstBlock, int lastBlock);
74 void FreeRulerMemory(int keepStart, int keepEnd);
76 std::shared_ptr<CFileItem> GetChannelItem(int iIndex) const { return m_channelItems[iIndex]; }
77 bool HasChannelItems() const { return !m_channelItems.empty(); }
78 int ChannelItemsSize() const { return static_cast<int>(m_channelItems.size()); }
79 int GetLastChannel() const
81 return m_channelItems.empty() ? -1 : static_cast<int>(m_channelItems.size()) - 1;
84 std::shared_ptr<CFileItem> GetRulerItem(int iIndex) const { return m_rulerItems[iIndex]; }
85 int RulerItemsSize() const { return static_cast<int>(m_rulerItems.size()); }
87 int GridItemsSize() const { return m_blocks; }
88 bool IsSameGridItem(int iChannel, int iBlock1, int iBlock2) const;
89 std::shared_ptr<CFileItem> GetGridItem(int iChannel, int iBlock) const;
90 int GetGridItemStartBlock(int iChannel, int iBlock) const;
91 int GetGridItemEndBlock(int iChannel, int iBlock) const;
92 CDateTime GetGridItemEndTime(int iChannel, int iBlock) const;
93 float GetGridItemWidth(int iChannel, int iBlock) const;
94 float GetGridItemOriginWidth(int iChannel, int iBlock) const;
95 void DecreaseGridItemWidth(int iChannel, int iBlock, float fSize);
97 bool IsZeroGridDuration() const { return (m_gridEnd - m_gridStart) == CDateTimeSpan(0, 0, 0, 0); }
98 const CDateTime& GetGridStart() const { return m_gridStart; }
99 const CDateTime& GetGridEnd() const { return m_gridEnd; }
100 unsigned int GetGridStartPadding() const;
102 unsigned int GetPageNowOffset() const;
103 int GetNowBlock() const;
104 int GetLastBlock() const { return m_blocks - 1; }
106 CDateTime GetStartTimeForBlock(int block) const;
107 int GetBlock(const CDateTime& datetime) const;
108 int GetFirstEventBlock(const std::shared_ptr<const CPVREpgInfoTag>& event) const;
109 int GetLastEventBlock(const std::shared_ptr<const CPVREpgInfoTag>& event) const;
110 bool IsEventMemberOfBlock(const std::shared_ptr<const CPVREpgInfoTag>& event, int iBlock) const;
112 std::unique_ptr<CFileItemList> GetCurrentTimeLineItems(int firstChannel, int numChannels) const;
114 private:
115 GridItem* GetGridItemPtr(int iChannel, int iBlock) const;
116 std::shared_ptr<CFileItem> CreateGapItem(int iChannel) const;
117 std::shared_ptr<CFileItem> GetItem(int iChannel, int iBlock) const;
119 std::vector<std::shared_ptr<CPVREpgInfoTag>> GetEPGTimeline(int iChannel,
120 const CDateTime& minEventEnd,
121 const CDateTime& maxEventStart) const;
123 struct EpgTags
125 std::vector<std::shared_ptr<CFileItem>> tags;
126 int firstBlock = -1;
127 int lastBlock = -1;
130 using EpgTagsMap = std::unordered_map<int, EpgTags>;
132 std::shared_ptr<CFileItem> CreateEpgTags(int iChannel, int iBlock) const;
133 std::shared_ptr<CFileItem> GetEpgTags(EpgTagsMap::iterator& itEpg,
134 int iChannel,
135 int iBlock) const;
136 std::shared_ptr<CFileItem> GetEpgTagsBefore(EpgTags& epgTags, int iChannel, int iBlock) const;
137 std::shared_ptr<CFileItem> GetEpgTagsAfter(EpgTags& epgTags, int iChannel, int iBlock) const;
139 mutable EpgTagsMap m_epgItems;
141 CDateTime m_gridStart;
142 CDateTime m_gridEnd;
144 std::vector<std::shared_ptr<CFileItem>> m_channelItems;
145 std::vector<std::shared_ptr<CFileItem>> m_rulerItems;
147 struct GridCoordinates
149 GridCoordinates(int _channel, int _block) : channel(_channel), block(_block) {}
151 bool operator==(const GridCoordinates& other) const
153 return (channel == other.channel && block == other.block);
156 int channel = 0;
157 int block = 0;
160 struct GridCoordinatesHash
162 std::size_t operator()(const GridCoordinates& coordinates) const
164 return std::hash<int>()(coordinates.channel) ^ std::hash<int>()(coordinates.block);
168 mutable std::unordered_map<GridCoordinates, GridItem, GridCoordinatesHash> m_gridIndex;
170 int m_blocks = 0;
171 float m_fBlockSize = 0.0f;
173 int m_firstActiveChannel = 0;
174 int m_lastActiveChannel = 0;
175 int m_firstActiveBlock = 0;
176 int m_lastActiveBlock = 0;
178 } // namespace PVR