1 #include "rootsmodel.h"
2 #include <iostream>
3 #include <QDebug>
4 
5 static double LOG2_10 = log(10) / log(2);
6 
7 namespace xmpsolve {
8 
RootsModel(QObject * parent)9 RootsModel::RootsModel(QObject *parent) :
10     QAbstractListModel(parent)
11 {
12     m_length = 0;
13     m_marked_root = -1;
14 }
15 
16 QHash<int, QByteArray>
roleNames() const17 RootsModel::roleNames() const
18 {
19     QHash<int, QByteArray> role_names;
20 
21     role_names.insert(RADIUS, "inclusion_radius");
22     role_names.insert(STATUS, "status");
23     role_names.insert(SHORT_APPROXIMATION, "short_approximation");
24     role_names.insert(ROOT, "root");
25     role_names.insert(MARKED, "marked");
26 
27     return role_names;
28 }
29 
30 int
rowCount(const QModelIndex & parent) const31 RootsModel::rowCount(const QModelIndex &parent) const
32 {
33     Q_UNUSED(parent);
34     return m_length;
35 }
36 
37 QVariant
data(const QModelIndex & index,int role) const38 RootsModel::data(const QModelIndex &index, int role) const
39 {
40     int i = index.row();
41 
42     if (role != Qt::DisplayRole && role < Qt::UserRole)
43         return QVariant();
44 
45     if (i < 0 || i > m_length)
46         return QVariant();
47     else
48     {
49 	cdpe_t croot;
50 	mpc_get_cdpe (croot, m_roots.at(i)->value);
51 	int real_digits = (rdpe_Esp (cdpe_Re (croot)) - rdpe_Esp (m_roots.at(i)->radius)) / LOG2_10 - 1;
52 	int imag_digits = (rdpe_Esp (cdpe_Im (croot)) - rdpe_Esp (m_roots.at(i)->radius)) / LOG2_10 - 1;
53 
54         char * buffer = NULL;
55 
56         int imag_digits_size = (imag_digits < 0) ? 2 : imag_digits;
57         int real_digits_size = (real_digits < 0) ? 2 : real_digits;
58 
59         switch (role)
60         {
61             case SHORT_APPROXIMATION:
62                 real_digits = (real_digits < 4) ? real_digits : 4;
63                 imag_digits = (imag_digits < 4) ? imag_digits : 4;
64 	        // fallthrough
65             case Qt::DisplayRole:
66 	        buffer = new char[real_digits_size + imag_digits_size + 20];
67 
68 		if (imag_digits <= 0 && real_digits <= 0)
69 		  gmp_sprintf (buffer, "0.0");
70 		else if (imag_digits <= 0)
71 		  gmp_sprintf (buffer, "%.*Fe", real_digits, mpc_Re (m_roots[i]->value));
72 		else if (real_digits <= 0)
73 		  gmp_sprintf (buffer, "%.*Fei", imag_digits, mpc_Im (m_roots[i]->value));
74 		else
75 		  {
76 		    if (m_roots[i]->get_imag_part() > 0)
77 		      gmp_sprintf (buffer, "%.*Fe + %.*Fei", real_digits, mpc_Re (m_roots[i]->value),
78 				   imag_digits, mpc_Im (m_roots[i]->value));
79 		    else
80 		      gmp_sprintf (buffer, "%.*Fe %.*Fei", real_digits, mpc_Re (m_roots[i]->value),
81 				   imag_digits, mpc_Im (m_roots[i]->value));
82 		  }
83 
84                 return QString(buffer);
85                 break;
86 
87             case STATUS:
88                 return QString(MPS_ROOT_STATUS_TO_STRING (m_roots[i]->status));
89 
90             case RADIUS:
91                 return QString("%1").arg(m_roots[i]->get_radius());
92 
93             case ROOT:
94                 return QVariant::fromValue((void*) m_roots[i]);
95 
96             case MARKED:
97                 return i == m_marked_root;
98 
99             default:
100                 qDebug() << "Invalid role";
101                 return QVariant();
102         }
103     }
104 }
105 
106 void
setRoots(QList<Root * > roots)107 RootsModel::setRoots(QList<Root *> roots)
108 {
109     beginResetModel();
110 
111     m_length = 0;
112     m_roots = roots;
113     m_length = roots.length();
114 
115     endResetModel();
116 }
117 
118 void
markRoot(int i)119 RootsModel::markRoot(int i)
120 {
121     int oldMarkedRoot = m_marked_root;
122 
123     if (i >= -1 && i < m_length)
124         m_marked_root = i;
125     else
126         m_marked_root = -1;
127 
128     if (oldMarkedRoot != -1)
129         dataChanged(index(oldMarkedRoot), index(oldMarkedRoot));
130     if (m_marked_root != -1)
131         dataChanged(index(m_marked_root), index(m_marked_root));
132 }
133 
134 
135 
136 } // namespace xmpsolve
137