|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * The source file for tone list model |
|
16 */ |
|
17 #include "tonefetchermodel.h" |
|
18 |
|
19 ToneFetcherModel::ToneFetcherModel(QObject *parent) |
|
20 : QStandardItemModel(parent) |
|
21 { |
|
22 } |
|
23 |
|
24 ToneFetcherModel::~ToneFetcherModel() |
|
25 { |
|
26 } |
|
27 |
|
28 QVariant ToneFetcherModel::data(const QModelIndex &index, int role) const |
|
29 { |
|
30 if (role == Qt::UserRole) { |
|
31 return mUserDataLst.at(index.row()); |
|
32 } else { |
|
33 return QStandardItemModel::data(index, role); |
|
34 } |
|
35 } |
|
36 |
|
37 void ToneFetcherModel::insertInOrder(QStandardItem *fileName, QStandardItem *filePath, int role) |
|
38 { |
|
39 QString name = fileName->text(); |
|
40 QString path = filePath->text(); |
|
41 int index = this->insertIndex(0, rowCount(), name); |
|
42 |
|
43 mUserDataLst.insert(index, path); |
|
44 QStandardItemModel::insertRow(index, fileName); |
|
45 } |
|
46 |
|
47 QString ToneFetcherModel::path(const QModelIndex &index) const |
|
48 { |
|
49 QString str = data(index, Qt::UserRole).toString(); |
|
50 return str; |
|
51 } |
|
52 |
|
53 int ToneFetcherModel::insertIndex(int low, int high, QString value) |
|
54 { |
|
55 if (low == high) { |
|
56 return low; |
|
57 } |
|
58 int middle = (low + high - 1)/2; |
|
59 QModelIndex lowItemIndex = ((QStandardItemModel *)this)->index(low, 0); |
|
60 QModelIndex highItemIndex = ((QStandardItemModel *)this)->index(high - 1, 0); |
|
61 QModelIndex middleItemIndex = (( QStandardItemModel *)this)->index(middle, 0); |
|
62 QString lowString = data(lowItemIndex).toString(); |
|
63 QString highString = data(highItemIndex).toString(); |
|
64 QString middleString = data(middleItemIndex).toString(); |
|
65 |
|
66 if (value >= highString) { |
|
67 return high; |
|
68 } |
|
69 if (value < lowString) { |
|
70 return low; |
|
71 } |
|
72 high = high - 1; |
|
73 while (low < high) { |
|
74 middle = (low + high)/2; |
|
75 middleItemIndex = ((QStandardItemModel *)this)->index(middle, 0); |
|
76 middleString = data(middleItemIndex).toString(); |
|
77 if (value >= middleString) { |
|
78 low = middle + 1; |
|
79 } else { |
|
80 high = middle; |
|
81 } |
|
82 } |
|
83 return low; |
|
84 } |
|
85 |
|
86 void ToneFetcherModel::refresh() |
|
87 { |
|
88 emit layoutChanged(); |
|
89 } |
|
90 |
|
91 void ToneFetcherModel::clearAll() |
|
92 { |
|
93 mUserDataLst.clear(); |
|
94 QStandardItemModel::clear(); |
|
95 } |
|
96 //End of File |