Initial Qt5 port
[harmattan/cameraplus] / declarative / videoresolutionmodel.cpp
1 /*!
2  * This file is part of CameraPlus.
3  *
4  * Copyright (C) 2012-2013 Mohammed Sameer <msameer@foolab.org>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
19  */
20
21 #include "videoresolutionmodel.h"
22 #include "qtcamvideosettings.h"
23 #include <QDebug>
24
25 VideoResolutionModel::VideoResolutionModel(QtCamVideoSettings *settings, QObject *parent) :
26   QAbstractListModel(parent), m_settings(settings) {
27
28   m_roles[IdRole] = "resolutionId";
29   m_roles[NameRole] = "resolutionName";
30   m_roles[CaptureRole] = "captureResolution";
31   m_roles[PreviewRole] = "previewResolution";
32   m_roles[FpsRole] = "frameRate";
33   m_roles[NightFpsRole] = "nightFrameRate";
34   m_roles[ResolutionRole] = "resolution";
35   m_roles[AspectRatioRole] = "resolutionAspectRatio";
36
37   m_resolutions = m_settings->resolutions(m_aspectRatio);
38 }
39
40 VideoResolutionModel::~VideoResolutionModel() {
41   m_settings = 0;
42 }
43
44 int VideoResolutionModel::rowCount(const QModelIndex& parent) const {
45   if (!parent.isValid()) {
46     return m_resolutions.size();
47   }
48
49   return 0;
50 }
51
52 QVariant VideoResolutionModel::data(const QModelIndex& index, int role) const {
53   if (index.row() < 0 || index.row() >= m_resolutions.size()) {
54     return QVariant();
55   }
56
57   const QtCamVideoResolution& res = m_resolutions[index.row()];
58
59   switch (role) {
60   case IdRole:
61     return res.id();
62
63   case NameRole:
64     return res.name();
65
66   case CaptureRole:
67     return res.captureResolution();
68
69   case PreviewRole:
70     return res.previewResolution();
71
72   case FpsRole:
73     return res.frameRate();
74
75   case NightFpsRole:
76     return res.nightFrameRate();
77
78   case ResolutionRole:
79     return res.resolution();
80
81   case AspectRatioRole:
82     return res.aspectRatio();
83
84   default:
85     return QVariant();
86   }
87 }
88
89 QString VideoResolutionModel::aspectRatio() const {
90   return m_aspectRatio;
91 }
92
93 void VideoResolutionModel::setAspectRatio(const QString& aspectRatio) {
94   if (aspectRatio != m_aspectRatio) {
95
96     m_aspectRatio = aspectRatio;
97
98     emit aspectRatioChanged();
99
100     beginResetModel();
101
102     m_resolutions = m_settings->resolutions(m_aspectRatio);
103
104     endResetModel();
105   }
106 }
107
108 QHash<int, QByteArray> VideoResolutionModel::roleNames() const {
109   return m_roles;
110 }