![]() |
TinkerCell Core 1.0
TinkerCell's Core library providing all basic functionalities
|
00001 /**************************************************************************** 00002 00003 Copyright (c) 2008 Deepak Chandran 00004 Contact: Deepak Chandran (dchandran1@gmail.com) 00005 See COPYRIGHT.TXT 00006 00007 This file defines a set of functions that can be used to convert C data types, 00008 such as char**, char*, and int to Qt data types, such as QStringList, QString, 00009 and QGraphicsItem. 00010 00011 ****************************************************************************/ 00012 00013 #include <stdio.h> 00014 #include <math.h> 00015 #include <string.h> 00016 #include <QtDebug> 00017 #include "ItemHandle.h" 00018 #include "MainWindow.h" 00019 #include "SymbolsTable.h" 00020 #include "ConvertValue.h" 00021 00022 namespace Tinkercell 00023 { 00024 00025 tc_matrix emptyMatrix() 00026 { 00027 tc_matrix m; 00028 m.values = 0; 00029 m.rownames.length = 0; 00030 m.colnames.length = 0; 00031 m.rownames.strings = 0; 00032 m.colnames.strings = 0; 00033 m.cols = 0; 00034 m.rows = 0; 00035 return m; 00036 } 00037 00038 ItemHandle* ConvertValue(long o) 00039 { 00040 MainWindow * main = MainWindow::instance(); 00041 if (main && main->isValidHandlePointer((void*)o)) 00042 return static_cast<ItemHandle*>((void*)o); 00043 return 0; 00044 } 00045 00046 long ConvertValue(ItemHandle* item) 00047 { 00048 void * p = (void*)item; 00049 return (long)(p); 00050 } 00051 00052 QList<ItemHandle*>* ConvertValue(tc_items A) 00053 { 00054 MainWindow * main = MainWindow::instance(); 00055 QList<ItemHandle*> * list = new QList<ItemHandle*>(); 00056 if (main) 00057 { 00058 for (int i=0; i < A.length; ++i) 00059 if (main->isValidHandlePointer((void*)(A.items[i]))) 00060 (*list) += static_cast<ItemHandle*>((void*)(A.items[i])); 00061 } 00062 return list; 00063 } 00064 00065 tc_items ConvertValue(const QList<ItemHandle*>& list) 00066 { 00067 tc_items A; 00068 void * p = 0; 00069 A.length = list.size(); 00070 A.items = 0; 00071 if (A.length > 0) 00072 { 00073 A.items = new long[A.length]; 00074 for (int i=0; i < list.size(); ++i) 00075 { 00076 p = (void*)list[i]; 00077 A.items[i] = (long)(p); 00078 } 00079 } 00080 return A; 00081 } 00082 00083 QString ConvertValue(const char* c) 00084 { 00085 return QString(c); 00086 } 00087 00088 const char* ConvertValue(const QString& s) 00089 { 00090 QByteArray text = s.toLocal8Bit(); 00091 char * data = new char[text.size() + 1]; 00092 strcpy(data, text.data()); 00093 return data; 00094 } 00095 00096 DataTable<qreal>* ConvertValue(tc_matrix m) 00097 { 00098 DataTable<qreal>* D = new DataTable<qreal>; 00099 if (m.rows < 0 || m.cols < 0) return D; 00100 D->resize(m.rows,m.cols); 00101 00102 for (int i=0; i < m.rows && m.rownames.strings && m.rownames.strings[i]; ++i) 00103 D->setRowName(i,ConvertValue(m.rownames.strings[i])); 00104 00105 for (int i=0; i < m.cols && m.colnames.strings && m.colnames.strings[i]; ++i) 00106 D->setColumnName(i,ConvertValue(m.colnames.strings[i])); 00107 00108 for (int i=0; i < m.rows; ++i) 00109 for (int j=0; j < m.cols; ++j) 00110 { 00111 D->value(i,j) = tc_getMatrixValue(m,i,j); 00112 } 00113 00114 return D; 00115 } 00116 00117 tc_matrix ConvertValue(const DataTable<qreal>& D) 00118 { 00119 tc_matrix m; 00120 00121 m.rownames.length = m.rows = D.rows(); 00122 m.colnames.length = m.cols = D.columns(); 00123 00124 if (m.rows > 0) 00125 m.rownames.strings = new char*[m.rows]; 00126 else 00127 m.rownames.strings = 0; 00128 00129 if (m.cols > 0) 00130 m.colnames.strings = new char*[m.cols]; 00131 else 00132 m.colnames.strings = 0; 00133 00134 if (m.rows > 0 && m.cols > 0) 00135 m.values = new double[m.rows * m.cols]; 00136 else 00137 m.values = 0; 00138 00139 for (int i=0; i < m.rows; ++i) 00140 { 00141 m.rownames.strings[i] = (char*)ConvertValue(D.rowName(i)); 00142 } 00143 00144 for (int i=0; i < m.cols; ++i) 00145 { 00146 m.colnames.strings[i] = (char*)ConvertValue(D.columnName(i)); 00147 } 00148 00149 for (int i=0; i < m.rows; ++i) 00150 for (int j=0; j < m.cols; ++j) 00151 { 00152 tc_setMatrixValue(m,i,j,D.at(i,j)); 00153 } 00154 00155 return m; 00156 } 00157 00158 DataTable<QString>* ConvertValue(tc_table m) 00159 { 00160 DataTable<QString>* D = new DataTable<QString>; 00161 if (m.rows < 0 || m.cols < 0) return D; 00162 D->resize(m.rows,m.cols); 00163 00164 for (int i=0; i < m.rows && m.rownames.strings && m.rownames.strings[i]; ++i) 00165 D->setRowName(i,ConvertValue(m.rownames.strings[i])); 00166 00167 for (int i=0; i < m.cols && m.colnames.strings && m.colnames.strings[i]; ++i) 00168 D->setColumnName(i,ConvertValue(m.colnames.strings[i])); 00169 00170 for (int i=0; i < m.rows; ++i) 00171 for (int j=0; j < m.cols; ++j) 00172 { 00173 D->value(i,j) = ConvertValue(tc_getTableValue(m,i,j)); 00174 } 00175 00176 return D; 00177 } 00178 00179 tc_table ConvertValue(const DataTable<QString>& D) 00180 { 00181 tc_table m; 00182 00183 m.rownames.length = m.rows = D.rows(); 00184 m.colnames.length = m.cols = D.columns(); 00185 00186 if (m.rows > 0) 00187 m.rownames.strings = new char*[m.rows]; 00188 else 00189 m.rownames.strings = 0; 00190 00191 if (m.cols > 0) 00192 m.colnames.strings = new char*[m.cols]; 00193 else 00194 m.colnames.strings = 0; 00195 00196 if (m.rows > 0 && m.cols > 0) 00197 m.strings = new char*[m.rows * m.cols]; 00198 else 00199 m.strings = 0; 00200 00201 for (int i=0; i < m.rows; ++i) 00202 { 00203 m.rownames.strings[i] = (char*)ConvertValue(D.rowName(i)); 00204 } 00205 00206 for (int i=0; i < m.cols; ++i) 00207 { 00208 m.colnames.strings[i] = (char*)ConvertValue(D.columnName(i)); 00209 } 00210 00211 for (int i=0; i < m.rows; ++i) 00212 for (int j=0; j < m.cols; ++j) 00213 { 00214 tc_setTableValue(m,i,j,(char*)ConvertValue(D.at(i,j))); 00215 } 00216 00217 return m; 00218 } 00219 00220 QStringList ConvertValue(tc_strings c) 00221 { 00222 QStringList slist; 00223 for (int i=0; i < c.length && c.strings && c.strings[i]; ++i) 00224 slist += ConvertValue(c.strings[i]); 00225 return slist; 00226 } 00227 00228 tc_strings ConvertValue(const QStringList& list) 00229 { 00230 tc_strings A; 00231 if (list.size() < 1) 00232 { 00233 A.length = 0; 00234 A.strings = 0; 00235 return A; 00236 } 00237 char ** cs = new char*[list.size()]; 00238 for (int i=0; i < list.size(); ++i) 00239 { 00240 cs[i] = (char*)ConvertValue(list[i]); 00241 } 00242 A.length = list.size(); 00243 A.strings = cs; 00244 return A; 00245 } 00246 } 00247